Backtestshugorun-20261013-011256#15169
resources/jsconfig: Address VS Code linting notice in jsconfig.json
Better
Our patch was judged better than the merged fix.
In this judgment, Patch B = our agent, Patch A = the merged fix.
The lint fires because a `paths` target is not relative while `baseUrl` is unset. TypeScript wants targets to start with `./` or `../`. Patch B fixes this at the root. Every target computed by `filepath.Rel` gets normalized to start with `./` or `../`. So `*` becomes `./*`, a subdirectory root becomes `./c/*`, and `../d/*` is left as is. The meaning of the mapping is kept: bare imports still resolve against the assets dir and the other source roots, so editor resolution keeps working. Patch A just skips the source root equal to `dir`. That has three problems: (1) It removes the self-mapping. In the common case (only the assets root) `Build` now returns nil and no jsconfig mapping is produced, so the editor can no longer resolve bare imports such as `imp1/index.js` from assets. That is a functional regression, not a fix. (2) It does not handle source roots that are subdirectories of `dir`. Those still yield non-relative targets like `c/*`, which trigger the same lint. (3) It hides the symptom by removing the mapping rather than correcting its format. On tests, Patch B adds a unit test covering the parent (`../`), same (`./*`) and child (`./c/*`) cases. It also adds an integration test checking that the generated `assets/jsconfig.json` contains `./*` and that the JS build still resolves the bare import. Patch A's test only asserts a nil config, which encodes the regression. The scope of both patches is comparable. Patch B's helper is small and clearly documented.
The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.
Solve
17m 21s
Wall-clock time in the container
Full test suite
No new failures
90 passed · 3 skipped · 93 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
47vs 11 merged
Changed lines, ours vs merged
The issue
View on GitHubWhen building a site that calls js.Build, the assets/jsconfig.json file looks like this:
{
"compilerOptions": {
"paths": {
"*": [
"*"
]
}
}
}
VS Code lints line 5 as follows:
Non-relative paths are not allowed when 'baseUrl' is not set. Did you forget a leading './'?
The two patches
Our patch+46 −1
resources/jsconfig/jsconfig.goindex 4aabf69f7..4defff66a 100644@@ -16,6 +16,7 @@ package jsconfig import ( "path/filepath" "sort"+ "strings" "github.com/bep/helpers/maphelpers" )@@ -41,7 +42,7 @@ func (b *Builder) Build(dir string) *Config { for root := range b.sourceRoots.All() { rel, err := filepath.Rel(dir, filepath.Join(root, "*")) if err == nil {- roots = append(roots, rel)+ roots = append(roots, toRelativePath(rel)) } } if len(roots) == 0 {@@ -53,6 +54,17 @@ func (b *Builder) Build(dir string) *Config { return conf } +// toRelativePath makes sure that rel, a path as returned by filepath.Rel,+// starts with "./" or "../" (or the OS specific equivalent).+// Non-relative paths are not allowed in paths when baseUrl is not set.+func toRelativePath(rel string) string {+ sep := string(filepath.Separator)+ if strings.HasPrefix(rel, "."+sep) || strings.HasPrefix(rel, ".."+sep) {+ return rel+ }+ return "." + sep + rel+}+ // AddSourceRoot adds a new source root. // This method is thread safe. func (b *Builder) AddSourceRoot(root string) {resources/jsconfig/jsconfig_test.goindex ab9b739d1..d258f73be 100644@@ -37,4 +37,12 @@ func TestJsConfigBuilder(t *testing.T) { c.Assert(string(data), qt.Not(qt.Contains), "baseUrl") c.Assert(NewBuilder().Build("/a/b"), qt.IsNil)++ // Non-relative paths are not allowed when baseUrl is not set.+ b = NewBuilder()+ b.AddSourceRoot("/a/b")+ b.AddSourceRoot("/a/b/c")+ b.AddSourceRoot("/a/d")+ conf = b.Build("/a/b")+ c.Assert(conf.CompilerOptions.Paths["*"], qt.DeepEquals, []string{filepath.FromSlash("../d/*"), filepath.FromSlash("./*"), filepath.FromSlash("./c/*")}) }resources/resource_transformers/js/js_integration_test.goindex 2244e8f8e..fa36cae15 100644@@ -14,6 +14,7 @@ package js_test import (+ "encoding/json" "os" "path/filepath" "strings"@@ -24,6 +25,7 @@ import ( "github.com/gohugoio/hugo/htesting" "github.com/gohugoio/hugo/hugolib" "github.com/gohugoio/hugo/internal/js/esbuild"+ "github.com/gohugoio/hugo/resources/jsconfig" ) func TestBuildVariants(t *testing.T) {@@ -448,3 +450,26 @@ JS Content:{{ $js.Content }}:End: b.Assert(err.Error(), qt.Contains, `execute of template failed: template: home.html:2:17`) b.Assert(err.Error(), qt.Contains, `main.js:1:31": Could not resolve "./util1"`) }++func TestJSConfigInAssetsPathsAreRelative(t *testing.T) {+ files := `+-- hugo.toml --+disableKinds=["page", "section", "taxonomy", "term", "sitemap", "robotsTXT"]+-- assets/imp1/index.js --+console.log("imp1");+-- assets/js/main.js --+import 'imp1/index.js';+-- layouts/home.html --+{{ $js := resources.Get "js/main.js" | js.Build }}+{{ $js.RelPermalink }}+`++ b := hugolib.Test(t, files, hugolib.TestOptOsFs())++ b.AssertFileContent("public/js/main.js", "imp1")+ // Non-relative paths are not allowed in paths when baseUrl is not set.+ var conf jsconfig.Config+ b.Assert(json.Unmarshal([]byte(b.FileContent("assets/jsconfig.json")), &conf), qt.IsNil)+ b.Assert(conf.CompilerOptions.Paths, qt.DeepEquals, map[string][]string{"*": {"./*"}})+ b.AssertFileContent("assets/jsconfig.json", "! baseUrl")+}
Merged fix · PR #+11 −0
resources/jsconfig/jsconfig.goindex 4aabf69f7..d347cab19 100644@@ -39,6 +39,11 @@ func (b *Builder) Build(dir string) *Config { } var roots []string for root := range b.sourceRoots.All() {+ if root == dir {+ // We need mappings for other source roots (e.g. mounted modules), but not for the current one.+ // See issue 15169.+ continue+ } rel, err := filepath.Rel(dir, filepath.Join(root, "*")) if err == nil { roots = append(roots, rel)resources/jsconfig/jsconfig_test.goindex ab9b739d1..1fb8ef3cd 100644@@ -37,4 +37,10 @@ func TestJsConfigBuilder(t *testing.T) { c.Assert(string(data), qt.Not(qt.Contains), "baseUrl") c.Assert(NewBuilder().Build("/a/b"), qt.IsNil)++ // Issue 15169.+ b = NewBuilder()+ b.AddSourceRoot("/c/assets")+ conf = b.Build("/c/assets")+ c.Assert(conf, qt.IsNil) }