Skip to content

Commit cb9e4f0

Browse files
leodidoona-agent
andcommitted
fix(yarn): handle scoped packages in link dependency extraction
Scoped npm packages (e.g., @scope/pkg) have an extra path component in the tarball structure. The extraction command now uses --strip-components=4 for scoped packages instead of 3. Path components: - Non-scoped: ./node_modules/pkg/ (3 components) - Scoped: ./node_modules/@scope/pkg/ (4 components) Co-authored-by: Ona <no-reply@ona.com>
1 parent 8b9566a commit cb9e4f0

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

pkg/leeway/build.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1698,8 +1698,14 @@ func (p *Package) buildYarn(buildctx *buildContext, wd, result string) (bld *pac
16981698
extractCmd = fmt.Sprintf("mkdir -p %s && tar -xzf %s -C %s --strip-components=1 package/", linkDepDir, builtPath, linkDepDir)
16991699
} else {
17001700
// YarnApp: extract ./node_modules/<pkg>/* to _link_deps/<pkg>/
1701-
// --strip-components=3 removes "./node_modules/<pkg>/" prefix
1702-
extractCmd = fmt.Sprintf("mkdir -p %s && tar -xzf %s -C %s --strip-components=3 ./node_modules/%s/", linkDepDir, builtPath, linkDepDir, npmName)
1701+
// --strip-components removes "./node_modules/<pkg>/" prefix
1702+
// For non-scoped packages (e.g., "utils"): 3 components (., node_modules, utils)
1703+
// For scoped packages (e.g., "@test/utils"): 4 components (., node_modules, @test, utils)
1704+
stripComponents := 3
1705+
if strings.HasPrefix(npmName, "@") {
1706+
stripComponents = 4
1707+
}
1708+
extractCmd = fmt.Sprintf("mkdir -p %s && tar -xzf %s -C %s --strip-components=%d ./node_modules/%s/", linkDepDir, builtPath, linkDepDir, stripComponents, npmName)
17031709
}
17041710
newRef = fmt.Sprintf("file:./%s", linkDepDir)
17051711

pkg/leeway/build_internal_test.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -272,19 +272,23 @@ func TestYarnAppExtraction_ScopedPackage(t *testing.T) {
272272
t.Fatalf("failed to create tarball: %v", err)
273273
}
274274

275-
// This is what the CURRENT production code does - always uses strip=3
276-
// For scoped packages, this is WRONG and should be strip=4
277-
const currentProductionStripComponents = 3
275+
// Calculate strip components based on package type:
276+
// - Non-scoped packages (e.g., "utils"): 3 components (., node_modules, utils)
277+
// - Scoped packages (e.g., "@test/utils"): 4 components (., node_modules, @test, utils)
278+
stripComponents := 3
279+
if strings.HasPrefix(tt.npmName, "@") {
280+
stripComponents = 4
281+
}
278282

279-
// Extract to _link_deps/<npmName>/ using the current production logic
283+
// Extract to _link_deps/<npmName>/ using the production logic
280284
extractDir := filepath.Join(tmpDir, "_link_deps", tt.npmName)
281285
if err := os.MkdirAll(extractDir, 0755); err != nil {
282286
t.Fatal(err)
283287
}
284288

285289
tarballFilter := fmt.Sprintf("./node_modules/%s/", tt.npmName)
286290
extractCmd := exec.Command("tar", "-xzf", tarballPath, "-C", extractDir,
287-
fmt.Sprintf("--strip-components=%d", currentProductionStripComponents), tarballFilter)
291+
fmt.Sprintf("--strip-components=%d", stripComponents), tarballFilter)
288292
if err := extractCmd.Run(); err != nil {
289293
t.Fatalf("extraction failed: %v", err)
290294
}

0 commit comments

Comments
 (0)