Skip to content

Commit 139f49a

Browse files
fredbiclaude
andcommitted
fix(genspec-wasi): resolve -workdir the way the host writes paths
An absolute -workdir was recognised by a leading slash, which is the WASI guest's rule and no host's. A Windows -workdir starts with a drive letter, so it read as relative and was appended to the working directory, and every scan asking for one failed to resolve its patterns against a directory that cannot exist. Absoluteness is now the host's own notion. Under WASI the two coincide, and the working directory is still consulted only for a relative -workdir, which is what the leading-slash test was there to avoid. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
1 parent 063865c commit 139f49a

2 files changed

Lines changed: 54 additions & 13 deletions

File tree

‎cmd/genspec-wasi/main.go‎

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import (
3636
"fmt"
3737
"io"
3838
"os"
39-
"strings"
39+
"path/filepath"
4040

4141
"github.com/go-openapi/codescan"
4242
"github.com/go-openapi/codescan/internal/exportdata"
@@ -220,24 +220,20 @@ func resolveLoader(mode string) (bool, error) {
220220
}
221221
}
222222

223-
// absolutePath resolves p against the working directory.
223+
// absolutePath resolves p against the working directory, in the host's own notion of a path.
224224
//
225-
// path/filepath.Abs is not used: it consults the process working directory, which a WASI guest may
226-
// not have, and the result has to be a slash path rooted at the mount anyway.
225+
// Absolute means what the host means by it, not "starts with a slash": a Windows -workdir starts
226+
// with a drive letter, so reading it as relative appends it to the working directory and yields a
227+
// path that names nothing. Under WASI the two notions coincide, and the working directory is
228+
// consulted only for a relative -workdir -- an absolute one names the mount and returns before
229+
// os.Getwd, which a guest need not have.
227230
func absolutePath(p string) (string, error) {
228-
if strings.HasPrefix(p, "/") {
229-
return p, nil
230-
}
231-
232-
wd, err := os.Getwd()
231+
abs, err := filepath.Abs(p)
233232
if err != nil {
234233
return "", fmt.Errorf("cannot resolve %q against the working directory: %w", p, err)
235234
}
236-
if p == "." || p == "" {
237-
return wd, nil
238-
}
239235

240-
return strings.TrimSuffix(wd, "/") + "/" + strings.TrimPrefix(p, "./"), nil
236+
return abs, nil
241237
}
242238

243239
func (c *config) emit(doc any, sink *collector, stdout io.Writer) error {

‎cmd/genspec-wasi/paths_test.go‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package main
5+
6+
import (
7+
"os"
8+
"path/filepath"
9+
"testing"
10+
11+
"github.com/go-openapi/testify/v2/assert"
12+
"github.com/go-openapi/testify/v2/require"
13+
)
14+
15+
// TestAbsolutePathLeavesAnAbsolutePathAlone is the Windows regression: -workdir is handed to the
16+
// toolchain-free loader as the place patterns resolve against, and a path recognised as relative is
17+
// appended to the working directory. "D:\tmp\x" does not start with a slash, so the leading-slash
18+
// rule made every absolute Windows -workdir name a directory that cannot exist.
19+
func TestAbsolutePathLeavesAnAbsolutePathAlone(t *testing.T) {
20+
t.Parallel()
21+
22+
dir := t.TempDir() // absolute in whatever shape this host writes
23+
24+
got, err := absolutePath(dir)
25+
require.NoError(t, err)
26+
assert.Equal(t, dir, got)
27+
}
28+
29+
// TestAbsolutePathAnchorsARelativePath covers the other half: what is relative is resolved, and the
30+
// answer is always absolute, since that is the whole contract the loader relies on.
31+
func TestAbsolutePathAnchorsARelativePath(t *testing.T) {
32+
t.Parallel()
33+
34+
wd, err := os.Getwd()
35+
require.NoError(t, err)
36+
37+
for _, p := range []string{".", "", "./models", "models", "a/../b"} {
38+
got, err := absolutePath(p)
39+
require.NoError(t, err)
40+
41+
assert.True(t, filepath.IsAbs(got), "%q resolved to %q, which is not absolute", p, got)
42+
assert.Equal(t, filepath.Join(wd, p), got, "%q", p)
43+
assert.Equal(t, filepath.Clean(got), got, "%q resolved to an uncleaned path", p)
44+
}
45+
}

0 commit comments

Comments
 (0)
⌂ 换网址