Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set working dir to the current one if test is read from stdin #3676

Merged
merged 2 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion js/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,14 @@ func newBundle(
CompatibilityMode: compatMode,
callableExports: make(map[string]struct{}),
filesystems: filesystems,
pwd: loader.Dir(src.URL),
pwd: src.PWD,
preInitState: piState,
}

if bundle.pwd == nil {
bundle.pwd = loader.Dir(src.URL)
}

c := bundle.newCompiler(piState.Logger)
bundle.ModuleResolver = modules.NewModuleResolver(getJSModules(), generateFileLoad(bundle), c)

Expand Down
1 change: 1 addition & 0 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
type SourceData struct {
Data []byte
URL *url.URL
PWD *url.URL
}

type loaderFunc func(logger logrus.FieldLogger, path string, parts []string) (string, error)
Expand Down
5 changes: 3 additions & 2 deletions loader/readsource.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
func ReadSource(
logger logrus.FieldLogger, src, pwd string, filesystems map[string]fsext.Fs, stdin io.Reader,
) (*SourceData, error) {
// 'ToSlash' is here as URL only use '/' as separators, but on Windows paths use '\'
pwdURL := &url.URL{Scheme: "file", Path: filepath.ToSlash(filepath.Clean(pwd)) + "/"}
mstoykov marked this conversation as resolved.
Show resolved Hide resolved
if src == "-" {
data, err := io.ReadAll(stdin)
if err != nil {
Expand All @@ -27,7 +29,7 @@ func ReadSource(
if err != nil {
return nil, fmt.Errorf("caching data read from -: %w", err)
}
return &SourceData{URL: &url.URL{Path: "/-", Scheme: "file"}, Data: data}, err
return &SourceData{URL: &url.URL{Path: "/-", Scheme: "file"}, Data: data, PWD: pwdURL}, err
}
var srcLocalPath string
if filepath.IsAbs(src) {
Expand All @@ -43,7 +45,6 @@ func ReadSource(
return Load(logger, filesystems, &url.URL{Scheme: "file", Path: filepath.ToSlash(srcLocalPath)}, src)
}

pwdURL := &url.URL{Scheme: "file", Path: filepath.ToSlash(filepath.Clean(pwd)) + "/"}
srcURL, err := Resolve(pwdURL, filepath.ToSlash(src))
if err != nil {
var unresolvedError unresolvableURLError
Expand Down
1 change: 1 addition & 0 deletions loader/readsource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestReadSourceSTDINCache(t *testing.T) {
require.Equal(t, &SourceData{
URL: &url.URL{Scheme: "file", Path: "/-"},
Data: data,
PWD: &url.URL{Scheme: "file", Path: "/path/to/pwd/"},
}, sourceData)
fileData, err := fsext.ReadFile(fs, "/-")
require.NoError(t, err)
Expand Down
Loading