-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change win program resolution to match macOS/Linux
After [some careful study](https://github.com/lread/info-process-builder/blob/main/README.adoc#program-resolution), I now understand the underlying behaviour of ProcessBuilder on Windows. It has some nuances: - it resolves from the current dir before PATH (which CMD Shell does too - but PowerShell opted not to do for security reasons) - it only resolves `a` to `a.exe` - and has what seem to be bugs wrt to resolving when a `directory` working dir is specified These oddities were sometimes leaking through to babashka process when run from Windows. We now entirely take over the program resolution for Windows in our default `:program-resolver` which now matches the behaviour of PowerShell (with the exception of launching `.ps1` files, we do not do that), macOS and Linux. Note that the default `:program-resolver` is also employed for `exec` on all OSes. Because it matches macOS/Linux behaviour this does not present any problems. Tests now thoroughly exercise program resolution via explicitly constructed scenarios instead of awkwardly relying on what happens to be on the PATH. See new test-resources dir README for some details. Bb tasks adjusted accordingly to setup the `PATH` for tests. New `dev:bb` and `dev:jvm` tasks added to launch nREPLs with this same setup. README is updated with new section on Program Resolution. Closes #163 Closes #164
- Loading branch information
Showing
16 changed files
with
618 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Babashka Process Test Resources | ||
|
||
For test coverage, we need executables that emit their path and their current working directory. | ||
For example, on Windows: | ||
``` | ||
> .\print-dirs.bat | ||
exepath: Z:\babashka\process\test-resources\print-dirs.bat | ||
workdir: Z:\babashka\process\test-resources | ||
> cd .. | ||
>test-resources\print-dirs | ||
exepath: Z:\babashka\process\test-resources\print-dirs.exe | ||
workdir: Z:\babashka\process | ||
``` | ||
|
||
This is straightforward for Linux/macOS `.sh`, Windows `.cmd` and Windows `.bat` variants, but for the Windows `.exe` variant, we need to create a binary. | ||
|
||
## Generating a Windows .exe | ||
|
||
It doesn't matter how `print-dirs.exe` is created; it won't need to be recreated often (we commit it to source control). | ||
|
||
Since it generates relatively small binaries very quickly for any architecture, we've built a `print-dirs.exe` using Go. | ||
Source is in [print-dirs.go](print-dirs.go) | ||
|
||
### Minimal Build | ||
Presuming you have [Go](https://go.dev/) installed, from this dir: | ||
|
||
```shell | ||
GOOS=windows GOARCH=amd64 go build -o print-dirs.exe print-dirs.go | ||
``` | ||
|
||
For me, this generated a 1.9mb `print-dirs.exe`. | ||
|
||
### Build a Smaller Exe | ||
Since this `.exe` is checked into version control, I opted to create a smaller binary by adding the following `-ldflags` to strip debug info: | ||
|
||
```shell | ||
GOOS=windows GOARCH=amd64 go build -ldflags "-s -w" -o print-dirs.exe print-dirs.go | ||
``` | ||
|
||
For me, the generated `print-dirs.exe` is now 1.3mb. | ||
To further shrink down the binary, you can use [UPX](https://upx.github.io/) (which is also cross-platform): | ||
|
||
``` | ||
upx --ultra-brute print-dirs.exe | ||
``` | ||
|
||
And now `print-dirs.exe` is 457kb. | ||
|
||
### Building With Docker | ||
If you don't want to install Go and UPX on your dev box but have docker installed, you can build from a docker image like so: | ||
|
||
``` shell | ||
docker run --rm \ | ||
-v "$PWD":/src \ | ||
-w /src \ | ||
devopsworks/golang-upx:latest \ | ||
bash -c 'GOOS=windows GOARCH=amd64 \ | ||
go build -ldflags "-s -w" -o print-dirs.exe print-dirs.go && | ||
upx --ultra-brute print-dirs.exe' | ||
``` | ||
|
||
This is ultimately the command I ran to create our Windows .exe. | ||
|
||
## What about Windows .com? | ||
|
||
These days, `.com` executables are rare. | ||
|
||
I could not find an easy way to create one. | ||
Our tests use `print-dirs.exe` copied to `print-dirs.com` to support `.com` test cases. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@echo off | ||
echo exepath: %~dpnx0 | ||
echo workdir: %cd% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@echo off | ||
echo exepath: %~dpnx0 | ||
echo workdir: %cd% |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
func main() { | ||
executable, _ := os.Executable() | ||
fmt.Fprintln(os.Stdout,"exepath:", executable) | ||
cwd, _ := os.Getwd() | ||
fmt.Fprintln(os.Stdout,"workdir:", cwd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Write-Output "exepath: $($MyInvocation.MyCommand.Path)" | ||
Write-Output "workdir: $(Get-Location)" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/usr/bin/env sh | ||
echo "exepath: $(readlink -f "$0")" | ||
echo "workdir: $(pwd)" |
Oops, something went wrong.