tinygo icon indicating copy to clipboard operation
tinygo copied to clipboard

Normalize expected path for chdir tests

Open QuLogic opened this issue 1 year ago • 2 comments

The expected path comes from a simple string concatenation in shell, but os.Getwd appears to have the normalized working directory, so this can fail if any parent directory is a symlink.

QuLogic avatar Dec 31 '24 07:12 QuLogic

For compatibility with non-module mode, we create a weird symlink in the equivalent directory path like this:

<mock-chroot> sh-5.3# pwd
/builddir/build/BUILD/tinygo-0.38.0-build/tinygo-0.38.0

<mock-chroot> sh-5.3# ls -lR _build
_build:
total 0
drwxr-xr-x. 1 mockbuild mock 20 Jul 20 15:20 src
_build/src:
total 0
drwxr-xr-x. 1 mockbuild mock 20 Jul 20 15:20 github.com
_build/src/github.com:
total 0
drwxr-xr-x. 1 mockbuild mock 12 Jul 20 15:20 tinygo-org
_build/src/github.com/tinygo-org:
total 4
lrwxrwxrwx. 1 mockbuild mock 55 Jul 20 15:20 tinygo -> /builddir/build/BUILD/tinygo-0.38.0-build/tinygo-0.38.0

That is, the tarball is extracted, and then _build/src/github.com/tinygo-org/tinygo in that same directory points back to it.

So when the smoke test runs, it errors like this:

+ cd _build/src/github.com/tinygo-org/tinygo
+ GOPATH=/builddir/build/BUILD/tinygo-0.38.0-build/tinygo-0.38.0/_build:/builddir/build/BUILD/tinygo-0.38.0-build/BUILDROOT/usr/lib64/tinygo:/usr/share/gocode
+ make smoketest STM32=0 XTENSA=0
# test 'build' command with{,out} -C argument
/builddir/build/BUILD/tinygo-0.38.0-build/BUILDROOT/usr/bin/tinygo build -C tests/testing/chdir chdir.go && rm tests/testing/chdir/chdir
/builddir/build/BUILD/tinygo-0.38.0-build/BUILDROOT/usr/bin/tinygo build ./tests/testing/chdir/chdir.go && rm chdir
# test 'run' command with{,out} -C argument
EXPECT_DIR=/builddir/build/BUILD/tinygo-0.38.0-build/tinygo-0.38.0/_build/src/github.com/tinygo-org/tinygo/tests/testing/chdir /builddir/build/BUILD/tinygo-0.38.0-build/BUILDROOT/usr/bin/tinygo run -C tests/testing/chdir chdir.go
2025/07/20 15:31:39 expected:"/builddir/build/BUILD/tinygo-0.38.0-build/tinygo-0.38.0/_build/src/github.com/tinygo-org/tinygo/tests/testing/chdir" != os.Getwd():"/builddir/build/BUILD/tinygo-0.38.0-build/tinygo-0.38.0/tests/testing/chdir"
failed to run compiled binary /tmp/tinygo295843607/main: exit status 1
make: *** [GNUmakefile:538: testchdir] Error 1

The EXPECT_DIR is generated by the Makefile with $(PWD), which is just the current directory of the shell, with no normalization performed. The docs for os.Getwd are not too specific, but they do say they return the absolute path. Since it is a wrapper for the getcwd call, those docs do state that "The pathname copied to the array shall contain no components that are symbolic links."

QuLogic avatar Jul 20 '25 20:07 QuLogic

This is one option, but wouldn't the appropriate place for this be in the Makefile? So that EXPECT_DIR is already correctly normalized?

aykevl avatar Oct 03 '25 10:10 aykevl