Skip to content

Commit

Permalink
fix #687: purge all uses of "path/filepath"
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jan 26, 2021
1 parent 46c9200 commit 88c8523
Show file tree
Hide file tree
Showing 11 changed files with 817 additions and 41 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ jobs:
- name: go vet
run: go vet ./cmd/... ./internal/... ./pkg/...

- name: Test for path/filepath
if: matrix.os == 'ubuntu-latest'
run: make no-filepath

- name: go fmt
if: matrix.os == 'macos-latest'
run: make fmt-go && git diff-index --quiet HEAD --
Expand Down
12 changes: 10 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,19 @@

This option was unintentionally broken when the internal JavaScript web worker source code was moved from an inline function to a string in version 0.5.20. The regression has been fixed and the `worker: false` scenario now has test coverage.

* Fix using stdin with the `esbuild-wasm` package on Windows ([#687](https://github.com/evanw/esbuild/issues/687))
* Fix absolute paths with the `esbuild-wasm` package on Windows ([#687](https://github.com/evanw/esbuild/issues/687))

The package `esbuild-wasm` has an `esbuild` command implemented using WebAssembly instead of using native code. It uses node's WebAssembly implementation and calls methods on node's `fs` module to access the file system.

Node has [an old bug ([nodejs/node#19831](https://github.com/nodejs/node/issues/19831), [nodejs/node#35997](https://github.com/nodejs/node/issues/35997)) where `fs.read` returns an EOF error at the end of stdin on Windows. This causes Go's WebAssembly implementation to panic when esbuild tries to read from stdin.
Go's `path/filepath` module has a bug where Windows paths are interpreted as Unix paths when targeting WebAssembly: [golang/go#43768](https://github.com/golang/go/issues/43768). This causes multiple issues including absolute paths such as `C:\path\to\file.js` being interpreted as relative paths (since they don't start with a `/`) and being joined onto the end of other paths.

To fix this, esbuild now does all of its own path handling instead of using Go's path handling code. The esbuild code base now contains a forked copy of `path/filepath` that can handle both Windows and Unix paths. The decision about which one to use is made at run-time. When targeting WebAssembly, the presence of the `C:\` directory is used to determine if Windows-style paths should be used.
With this release, it should now be possible to use Windows-style paths with esbuild's WebAssembly implementation on Windows.

* Fix using stdin with the `esbuild-wasm` package on Windows ([#687](https://github.com/evanw/esbuild/issues/687))

Node has an old bug ([nodejs/node#19831](https://github.com/nodejs/node/issues/19831), [nodejs/node#35997](https://github.com/nodejs/node/issues/35997)) where `fs.read` returns an EOF error at the end of stdin on Windows. This causes Go's WebAssembly implementation to panic when esbuild tries to read from stdin.

The workaround was to manually check for this case and then ignore the error in this specific case. With this release, it should now be possible to pipe something to the `esbuild` command on Windows.

Expand Down
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test:
make -j6 test-common

# These tests are for development
test-common: test-go vet-go verify-source-map end-to-end-tests js-api-tests plugin-tests register-test
test-common: test-go vet-go no-filepath verify-source-map end-to-end-tests js-api-tests plugin-tests register-test

# These tests are for release (the extra tests are not included in "test" because they are pretty slow)
test-all:
Expand All @@ -28,6 +28,10 @@ vet-go:
fmt-go:
go fmt ./cmd/... ./internal/... ./pkg/...

no-filepath:
@! grep --color --include '*.go' -r '"path/filepath"' cmd internal pkg || ( \
echo 'error: Use of "path/filepath" is disallowed. See http://golang.org/issue/43768.' && false)

test-wasm-node: esbuild
PATH="$(shell go env GOROOT)/misc/wasm:$$PATH" GOOS=js GOARCH=wasm go test ./internal/...
node scripts/wasm-tests.js
Expand Down
Loading

0 comments on commit 88c8523

Please sign in to comment.