Skip to content

Commit

Permalink
link local npm version to separate folder to add it to the path
Browse files Browse the repository at this point in the history
Co-authored-by: Ralf Pannemans <[email protected]>
  • Loading branch information
nicolasbender and c0d1ngm0nk3y committed Aug 15, 2024
1 parent db6aa58 commit 26d6f61
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 39 deletions.
26 changes: 18 additions & 8 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package npminstall
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
"time"
Expand Down Expand Up @@ -94,11 +95,20 @@ func Build(entryResolver EntryResolver,
Stdout: logger.ActionWriter,
Stderr: logger.ActionWriter,
})
moduleBinPath := filepath.Join(projectPath, "node_modules", ".bin")
os.Setenv("PATH", fmt.Sprintf("%s:%s:%s", filepath.Join(moduleBinPath, "npm"), os.Getenv("PATH"), moduleBinPath))
if err != nil {
return packit.BuildResult{}, fmt.Errorf("update of npm failed: %w", err)
}
moduleBinPath := filepath.Join(projectPath, "node_modules", ".bin")
localBinPath := filepath.Join(projectPath, "node_modules", ".bin_local")
err = os.Mkdir(localBinPath, os.ModePerm)
if err != nil {
return packit.BuildResult{}, err
}
err = os.Link(path.Join(moduleBinPath, "npm"), filepath.Join(localBinPath, "npm"))
if err != nil {
return packit.BuildResult{}, err
}
os.Setenv("PATH", fmt.Sprintf("%s:%s:%s", filepath.Join(localBinPath), os.Getenv("PATH"), moduleBinPath))
}

npmCacheLayer, err := context.Layers.Get(LayerNameCache)
Expand Down Expand Up @@ -176,9 +186,9 @@ func Build(entryResolver EntryResolver,
if globalNpmrcPath != "" {
layer.BuildEnv.Default("NPM_CONFIG_GLOBALCONFIG", globalNpmrcPath)
}
path := filepath.Join(layer.Path, "node_modules", ".bin")
layer.BuildEnv.Append("PATH", path, string(os.PathListSeparator))
layer.BuildEnv.Prepend("PATH", filepath.Join(path, "npm"), string(os.PathListSeparator))
nodeModulesPath := filepath.Join(layer.Path, "node_modules")
layer.BuildEnv.Append("PATH", filepath.Join(nodeModulesPath, ".bin"), string(os.PathListSeparator))
layer.BuildEnv.Prepend("PATH", filepath.Join(nodeModulesPath, ".bin_local"), string(os.PathListSeparator))
layer.BuildEnv.Override("NODE_ENV", "development")

logger.EnvironmentVariables(layer)
Expand Down Expand Up @@ -293,9 +303,9 @@ func Build(entryResolver EntryResolver,

layer.LaunchEnv.Default("NPM_CONFIG_LOGLEVEL", "error")
layer.LaunchEnv.Default("NODE_PROJECT_PATH", projectPath)
path := filepath.Join(layer.Path, "node_modules", ".bin")
layer.LaunchEnv.Append("PATH", path, string(os.PathListSeparator))
layer.LaunchEnv.Prepend("PATH", filepath.Join(path, "npm"), string(os.PathListSeparator))
nodeModulesPath := filepath.Join(layer.Path, "node_modules")
layer.LaunchEnv.Append("PATH", filepath.Join(nodeModulesPath, ".bin"), string(os.PathListSeparator))
layer.LaunchEnv.Prepend("PATH", filepath.Join(nodeModulesPath, ".bin_local"), string(os.PathListSeparator))

logger.EnvironmentVariables(layer)

Expand Down
8 changes: 4 additions & 4 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
Expect(buildLayer.SharedEnv).To(Equal(packit.Environment{}))
Expect(buildLayer.BuildEnv).To(Equal(packit.Environment{
"PATH.append": filepath.Join(layersDir, "build-modules", "node_modules", ".bin"),
"PATH.prepend": filepath.Join(layersDir, "build-modules", "node_modules", ".bin", "npm"),
"PATH.prepend": filepath.Join(layersDir, "build-modules", "node_modules", ".bin_local"),
"PATH.delim": ":",
"NODE_ENV.override": "development",
}))
Expand Down Expand Up @@ -308,7 +308,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
"NPM_CONFIG_LOGLEVEL.default": "error",
"NODE_PROJECT_PATH.default": workingDir,
"PATH.append": filepath.Join(layersDir, "launch-modules", "node_modules", ".bin"),
"PATH.prepend": filepath.Join(layersDir, "launch-modules", "node_modules", ".bin", "npm"),
"PATH.prepend": filepath.Join(layersDir, "launch-modules", "node_modules", ".bin_local"),
"PATH.delim": ":",
}))
Expect(launchLayer.ProcessLaunchEnv).To(Equal(map[string]packit.Environment{}))
Expand Down Expand Up @@ -461,7 +461,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
Expect(buildLayer.SharedEnv).To(Equal(packit.Environment{}))
Expect(buildLayer.BuildEnv).To(Equal(packit.Environment{
"PATH.append": filepath.Join(layersDir, "build-modules", "node_modules", ".bin"),
"PATH.prepend": filepath.Join(layersDir, "build-modules", "node_modules", ".bin", "npm"),
"PATH.prepend": filepath.Join(layersDir, "build-modules", "node_modules", ".bin_local"),
"PATH.delim": ":",
"NODE_ENV.override": "development",
}))
Expand Down Expand Up @@ -557,7 +557,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
"NPM_CONFIG_LOGLEVEL.default": "error",
"NODE_PROJECT_PATH.default": workingDir,
"PATH.append": filepath.Join(layersDir, "launch-modules", "node_modules", ".bin"),
"PATH.prepend": filepath.Join(layersDir, "launch-modules", "node_modules", ".bin", "npm"),
"PATH.prepend": filepath.Join(layersDir, "launch-modules", "node_modules", ".bin_local"),
"PATH.delim": ":",
}))
Expect(launchLayer.ProcessLaunchEnv).To(Equal(map[string]packit.Environment{}))
Expand Down
4 changes: 2 additions & 2 deletions integration/caching_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,12 @@ func testCaching(t *testing.T, context spec.G, it spec.S) {
fmt.Sprintf(extenderBuildStr+" Running 'npm ci --unsafe-perm --cache /layers/%s/npm-cache'", strings.ReplaceAll(settings.Buildpack.ID, "/", "_")),
))
Expect(logs).To(ContainLines(MatchRegexp(extenderBuildStrEscaped + ` Completed in (\d+\.\d+|\d{3})`)))
moduleBinPath := fmt.Sprintf("/layers/%s/launch-modules/node_modules/.bin", strings.ReplaceAll(settings.Buildpack.ID, "/", "_"))
modulePath := fmt.Sprintf("/layers/%s/launch-modules/node_modules", strings.ReplaceAll(settings.Buildpack.ID, "/", "_"))
Expect(logs).To(ContainLines(
extenderBuildStr+" Configuring launch environment",
extenderBuildStr+" NODE_PROJECT_PATH -> \"/workspace\"",
extenderBuildStr+" NPM_CONFIG_LOGLEVEL -> \"error\"",
fmt.Sprintf(extenderBuildStr+" PATH -> \"%s/npm:$PATH:%s\"", moduleBinPath, moduleBinPath),
fmt.Sprintf(extenderBuildStr+" PATH -> \"%s/.bin_local:$PATH:%s/.bin\"", modulePath, modulePath),
extenderBuildStr+"",
))

Expand Down
30 changes: 15 additions & 15 deletions integration/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +121,22 @@ func TestIntegration(t *testing.T) {
settings.Buildpacks.NPMList.Online = filepath.Join(root, "integration", "testdata", "npm-list-buildpack")

suite := spec.New("Integration", spec.Parallel(), spec.Report(report.Terminal{}))
// suite("Caching", testCaching)
// suite("DevDependenciesDuringBuild", testDevDependenciesDuringBuild)
// suite("EmptyNodeModules", testEmptyNodeModules)
// suite("Logging", testLogging)
// suite("NativeModules", testNativeModules)
// suite("NoNodeModules", testNoNodeModules)
// suite("Npmrc", testNpmrc)
// suite("PackageLockMismatch", testPackageLockMismatch)
// suite("PrePostScriptsRebuild", testPrePostScriptRebuild)
// suite("ProjectPath", testProjectPath)
// suite("Restart", testRestart)
suite("Caching", testCaching)
suite("DevDependenciesDuringBuild", testDevDependenciesDuringBuild)
suite("EmptyNodeModules", testEmptyNodeModules)
suite("Logging", testLogging)
suite("NativeModules", testNativeModules)
suite("NoNodeModules", testNoNodeModules)
suite("Npmrc", testNpmrc)
suite("PackageLockMismatch", testPackageLockMismatch)
suite("PrePostScriptsRebuild", testPrePostScriptRebuild)
suite("ProjectPath", testProjectPath)
suite("Restart", testRestart)
suite("SimpleApp", testSimpleApp)
// suite("UnmetDependencies", testUnmetDependencies)
suite("UnmetDependencies", testUnmetDependencies)
suite("Vendored", testVendored)
// suite("VendoredWithBinaries", testVendoredWithBinaries)
// suite("Versioning", testVersioning)
// suite("Workspaces", testWorkspaces)
suite("VendoredWithBinaries", testVendoredWithBinaries)
suite("Versioning", testVersioning)
suite("Workspaces", testWorkspaces)
suite.Run(t)
}
12 changes: 6 additions & 6 deletions integration/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ func testLogging(t *testing.T, context spec.G, it spec.S) {
Expect(logs).To(ContainLines(
MatchRegexp(extenderBuildStrEscaped + ` Completed in (\d+\.\d+|\d{3})`),
))
moduleBinPath := fmt.Sprintf("/layers/%s/launch-modules/node_modules/.bin", strings.ReplaceAll(settings.Buildpack.ID, "/", "_"))
modulePath := fmt.Sprintf("/layers/%s/launch-modules/node_modules", strings.ReplaceAll(settings.Buildpack.ID, "/", "_"))
Expect(logs).To(ContainLines(
extenderBuildStr+" Configuring launch environment",
extenderBuildStr+" NODE_PROJECT_PATH -> \"/workspace\"",
extenderBuildStr+" NPM_CONFIG_LOGLEVEL -> \"error\"",
fmt.Sprintf(extenderBuildStr+" PATH -> \"%s/npm:$PATH:%s\"", moduleBinPath, moduleBinPath),
fmt.Sprintf(extenderBuildStr+" PATH -> \"%s/.bin_local$PATH:%s/.bin\"", modulePath, modulePath),
))
})

Expand Down Expand Up @@ -138,11 +138,11 @@ func testLogging(t *testing.T, context spec.G, it spec.S) {
Expect(logs).To(ContainLines(
MatchRegexp(extenderBuildStrEscaped + ` Completed in (\d+\.\d+|\d{3})`),
))
moduleBinPath := fmt.Sprintf("/layers/%s/build-modules/node_modules/.bin", strings.ReplaceAll(settings.Buildpack.ID, "/", "_"))
modulePath := fmt.Sprintf("/layers/%s/build-modules/node_modules", strings.ReplaceAll(settings.Buildpack.ID, "/", "_"))
Expect(logs).To(ContainLines(
extenderBuildStr+" Configuring build environment",
extenderBuildStr+" NODE_ENV -> \"development\"",
fmt.Sprintf(extenderBuildStr+" PATH -> \"%s/npm:$PATH:%s\"", moduleBinPath, moduleBinPath),
fmt.Sprintf(extenderBuildStr+" PATH -> \"%s/.bin_local:$PATH:%s/.bin\"", modulePath, modulePath),
extenderBuildStr+"",
fmt.Sprintf(extenderBuildStr+` Generating SBOM for /layers/%s/build-modules`, strings.ReplaceAll(settings.Buildpack.ID, "/", "_")),
MatchRegexp(extenderBuildStrEscaped+` Completed in (\d+)(\.\d+)?(ms|s)`),
Expand All @@ -151,12 +151,12 @@ func testLogging(t *testing.T, context spec.G, it spec.S) {
extenderBuildStr+" Executing launch environment install process",
extenderBuildStr+" Running 'npm prune'",
))
moduleBinPath = fmt.Sprintf("/layers/%s/launch-modules/node_modules/.bin", strings.ReplaceAll(settings.Buildpack.ID, "/", "_"))
modulePath = fmt.Sprintf("/layers/%s/launch-modules/node_modules", strings.ReplaceAll(settings.Buildpack.ID, "/", "_"))
Expect(logs).To(ContainLines(
extenderBuildStr+" Configuring launch environment",
extenderBuildStr+" NODE_PROJECT_PATH -> \"/workspace\"",
extenderBuildStr+" NPM_CONFIG_LOGLEVEL -> \"error\"",
fmt.Sprintf(extenderBuildStr+" PATH -> \"%s/npm:$PATH:%s\"", moduleBinPath, moduleBinPath),
fmt.Sprintf(extenderBuildStr+" PATH -> \"%s/.bin_local:$PATH:%s/.bin\"", modulePath, modulePath),
extenderBuildStr+"",
fmt.Sprintf(extenderBuildStr+` Generating SBOM for /layers/%s/launch-modules`, strings.ReplaceAll(settings.Buildpack.ID, "/", "_")),
MatchRegexp(extenderBuildStrEscaped+` Completed in (\d+)(\.\d+)?(ms|s)`),
Expand Down
4 changes: 2 additions & 2 deletions integration/native_modules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ func testNativeModules(t *testing.T, context spec.G, it spec.S) {
Expect(logs).To(ContainLines(MatchRegexp(` Running 'npm rebuild --nodedir=/layers/.+/node'`)))
}
Expect(logs).To(ContainLines(extenderBuildStr + " Running 'npm run-script postinstall --if-present'"))
moduleBinPath := fmt.Sprintf("/layers/%s/launch-modules/node_modules/.bin", strings.ReplaceAll(settings.Buildpack.ID, "/", "_"))
modulePath := fmt.Sprintf("/layers/%s/launch-modules/node_modules", strings.ReplaceAll(settings.Buildpack.ID, "/", "_"))
Expect(logs).To(ContainLines(
extenderBuildStr+" Configuring launch environment",
extenderBuildStr+" NODE_PROJECT_PATH -> \"/workspace\"",
extenderBuildStr+" NPM_CONFIG_LOGLEVEL -> \"error\"",
fmt.Sprintf(extenderBuildStr+" PATH -> \"%s/npm:$PATH:%s\"", moduleBinPath, moduleBinPath),
fmt.Sprintf(extenderBuildStr+" PATH -> \"%s/.bin_local:$PATH:%s/.bin\"", modulePath, modulePath),
extenderBuildStr+"",
))
})
Expand Down
4 changes: 2 additions & 2 deletions integration/vendored_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ func testVendored(t *testing.T, context spec.G, it spec.S) {
Expect(logs).To(ContainLines(MatchRegexp(` Running 'npm rebuild --nodedir=/layers/.+/node'`)))
}
Expect(logs).To(ContainLines(extenderBuildStr + " Running 'npm run-script postinstall --if-present'"))
moduleBinPath := fmt.Sprintf("/layers/%s/launch-modules/node_modules/.bin", strings.ReplaceAll(settings.Buildpack.ID, "/", "_"))
modulePath := fmt.Sprintf("/layers/%s/launch-modules/node_modules", strings.ReplaceAll(settings.Buildpack.ID, "/", "_"))
Expect(logs).To(ContainLines(
extenderBuildStr+" Configuring launch environment",
extenderBuildStr+" NODE_PROJECT_PATH -> \"/workspace\"",
extenderBuildStr+" NPM_CONFIG_LOGLEVEL -> \"error\"",
fmt.Sprintf(extenderBuildStr+" PATH -> \"%s/npm:$PATH:%s\"", moduleBinPath, moduleBinPath),
fmt.Sprintf(extenderBuildStr+" PATH -> \"%s/.bin_local:$PATH:%s/.bin\"", modulePath, modulePath),
extenderBuildStr+"",
))
})
Expand Down

0 comments on commit 26d6f61

Please sign in to comment.