Skip to content

Commit

Permalink
Fix paths with spaces or other url escaped characters
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Jun 24, 2024
1 parent 9b42cb9 commit befa609
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
5 changes: 4 additions & 1 deletion js/modules/require_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ func (r *LegacyRequireImpl) CurrentlyRequiredModule() (*url.URL, error) {
case strings.HasPrefix(fileStr, "file://"):
u = new(url.URL)
u.Scheme = "file"
u.Path = strings.TrimPrefix(fileStr, "file://")
u.Path, err = url.PathUnescape(strings.TrimPrefix(fileStr, "file://"))
if err != nil {
return nil, err
}
case strings.HasPrefix(fileStr, "https://"):
var err error
u, err = url.Parse(fileStr)
Expand Down
19 changes: 19 additions & 0 deletions js/path_resolution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,25 @@ func TestOpenPathResolution(t *testing.T) {
`,
},
},
"space in path": {
fsMap: map[string]any{
"/A/B D/data.txt": "data file",
"/A/C D/B/script.js": `
// Here the path is relative to this module but to the one calling
module.exports = () => open("./../data.txt");
`,
"/A/B D/B/script.js": `
module.exports = require("./../../C D/B/script.js")();
`,
"/A/A/A/A/script.js": `
let data = require("./../../../B D/B/script.js");
if (data != "data file") {
throw new Error("wrong content " + data);
}
export default function() {}
`,
},
},
}

for name, testCase := range testCases {
Expand Down

0 comments on commit befa609

Please sign in to comment.