diff --git a/CHANGELOG_NEXT.md b/CHANGELOG_NEXT.md index 9c3841ce8..b61848bed 100644 --- a/CHANGELOG_NEXT.md +++ b/CHANGELOG_NEXT.md @@ -21,6 +21,12 @@ This CHANGELOG describes the merged but unreleased changes. Please see [CHANGELO ### Compiler changes +#### NodeJS Backend + +* The NodeJS executable output to `build/exec/` now has its executable bit set. + That file already had a NodeJS shebang at the top, so now it is fully ready to + go after compilation. + ### Library changes #### Prelude diff --git a/src/Compiler/ES/Node.idr b/src/Compiler/ES/Node.idr index 3d67689be..320385647 100644 --- a/src/Compiler/ES/Node.idr +++ b/src/Compiler/ES/Node.idr @@ -14,17 +14,21 @@ import Core.TT import Libraries.Utils.Path import System +import System.File.Permissions import Data.Maybe %default covering +envNode : String +envNode = "/usr/bin/env node" + findNode : IO String findNode = do Nothing <- idrisGetEnv "NODE" | Just node => pure node path <- pathLookup ["node"] - pure $ fromMaybe "/usr/bin/env node" path + pure $ fromMaybe envNode path ||| Compile a TT expression to Node compileToNode : @@ -36,7 +40,7 @@ compileToNode c s tm = do pure $ shebang ++ js where shebang : String - shebang = "#!/usr/bin/env node\n" + shebang = "#!\{envNode}\n" ||| Node implementation of the `compileExpr` interface. compileExpr : @@ -51,6 +55,7 @@ compileExpr c s tmpDir outputDir tm outfile = do es <- compileToNode c s tm let out = outputDir outfile Core.writeFile out es + coreLift_ $ chmodRaw out 0o755 pure (Just out) ||| Node implementation of the `executeExpr` interface. diff --git a/tests/node/executable/TestExecutable.idr b/tests/node/executable/TestExecutable.idr new file mode 100644 index 000000000..29bda8f53 --- /dev/null +++ b/tests/node/executable/TestExecutable.idr @@ -0,0 +1,6 @@ +module TestExecutable + +import System + +main : IO () +main = putStrLn "Hi there!" diff --git a/tests/node/executable/expected b/tests/node/executable/expected new file mode 100644 index 000000000..c9f384789 --- /dev/null +++ b/tests/node/executable/expected @@ -0,0 +1,2 @@ +Hi there! +Hi there! diff --git a/tests/node/executable/run b/tests/node/executable/run new file mode 100644 index 000000000..c8d3201f1 --- /dev/null +++ b/tests/node/executable/run @@ -0,0 +1,19 @@ +. ../../testutils.sh + +idris2 --cg node -o node_executable TestExecutable.idr > /dev/null + +# node still executes it: +node ./build/exec/node_executable + +# it can be executed on its own due to shebang and executable bit: +# (/usr/bin/env as used in shebang is problematic in edge cases +# but still generally a good bet; we will make sure the test works +# even when /usr/bin/env cannot be used) +fallback () { + nodejs="$(command -v node)" + # cross-platform supported sed-ing (unlike with in-place option) + cp ./build/exec/node_executable ./build/exec/node_executable_src + sed "s#/usr/bin/env node#$nodejs#" ./build/exec/node_executable_src > ./build/exec/node_executable + ./build/exec/node_executable +} +./build/exec/node_executable || fallback