-
Notifications
You must be signed in to change notification settings - Fork 144
/
main.go
254 lines (230 loc) · 8.14 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Implements nodejs/npm buildpack.
// The npm buildpack installs dependencies using npm.
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/GoogleCloudPlatform/buildpacks/pkg/ar"
"github.com/GoogleCloudPlatform/buildpacks/pkg/buildermetrics"
"github.com/GoogleCloudPlatform/buildpacks/pkg/cache"
"github.com/GoogleCloudPlatform/buildpacks/pkg/devmode"
gcp "github.com/GoogleCloudPlatform/buildpacks/pkg/gcpbuildpack"
"github.com/GoogleCloudPlatform/buildpacks/pkg/nodejs"
)
const (
cacheTag = "prod dependencies"
)
func main() {
gcp.Main(detectFn, buildFn)
}
func detectFn(ctx *gcp.Context) (gcp.DetectResult, error) {
pkgJSONExists, err := ctx.FileExists("package.json")
if err != nil {
return nil, err
}
if !pkgJSONExists {
return gcp.OptOutFileNotFound("package.json"), nil
}
return gcp.OptInFileFound("package.json"), nil
}
func buildFn(ctx *gcp.Context) error {
ml, err := ctx.Layer("npm_modules", gcp.BuildLayer, gcp.CacheLayer)
if err != nil {
return fmt.Errorf("creating layer: %w", err)
}
nm := filepath.Join(ml.Path, "node_modules")
if nmExists, _ := ctx.FileExists("node_modules"); nmExists {
buildermetrics.GlobalBuilderMetrics().GetCounter(buildermetrics.NpmNodeModulesCounterID).Increment(1)
}
vendorNpmDeps := nodejs.IsUsingVendoredDependencies()
if !vendorNpmDeps {
if err := ctx.RemoveAll("node_modules"); err != nil {
return err
}
}
if err := ar.GenerateNPMConfig(ctx); err != nil {
return fmt.Errorf("generating Artifact Registry credentials: %w", err)
}
pjs, err := nodejs.ReadPackageJSONIfExists(ctx.ApplicationRoot())
if err != nil {
return err
}
if err := upgradeNPM(ctx, pjs); err != nil {
vendorError := ""
if vendorNpmDeps {
vendorError = "Vendored dependencies detected, please remove the npm version from your package.json to avoid installing npm and instead use the bundled npm"
}
return fmt.Errorf("%s Error: %w", vendorError, err)
}
lockfile, err := nodejs.EnsureLockfile(ctx)
if err != nil {
return err
}
buildCmds, isCustomBuild := nodejs.DetermineBuildCommands(pjs, "npm")
// Respect the user's NODE_ENV value if it's set
buildNodeEnv, nodeEnvPresent := os.LookupEnv(nodejs.EnvNodeEnv)
if !nodeEnvPresent {
if len(buildCmds) > 0 {
// Assume that dev dependencies are required to run build scripts to
// support the most use cases possible.
buildNodeEnv = nodejs.EnvDevelopment
} else {
buildNodeEnv = nodejs.EnvProduction
}
}
if vendorNpmDeps {
buildermetrics.GlobalBuilderMetrics().GetCounter(buildermetrics.NpmVendorDependenciesCounterID).Increment(1)
if _, err := ctx.Exec([]string{"npm", "rebuild"}, gcp.WithEnv("NODE_ENV="+buildNodeEnv), gcp.WithUserAttribution); err != nil {
return err
}
} else {
cached, err := nodejs.CheckOrClearCache(ctx, ml, cache.WithStrings(buildNodeEnv), cache.WithFiles("package.json", lockfile))
if err != nil {
return fmt.Errorf("checking cache: %w", err)
}
if cached {
// Restore cached node_modules.
if _, err := ctx.Exec([]string{"cp", "--archive", nm, "node_modules"}, gcp.WithUserTimingAttribution); err != nil {
return err
}
// Always run npm install to run preinstall/postinstall scripts.
// Otherwise it should be a no-op because the lockfile is unchanged.
if _, err := ctx.Exec([]string{"npm", "install", "--quiet"}, gcp.WithEnv("NODE_ENV="+buildNodeEnv), gcp.WithUserAttribution); err != nil {
return err
}
} else {
ctx.Logf("Installing application dependencies.")
installCmd, err := nodejs.NPMInstallCommand(ctx)
if err != nil {
return err
}
if _, err := ctx.Exec([]string{"npm", installCmd, "--quiet"}, gcp.WithEnv("NODE_ENV="+buildNodeEnv), gcp.WithUserAttribution); err != nil {
return err
}
// Ensure node_modules exists even if no dependencies were installed.
if err := ctx.MkdirAll("node_modules", 0755); err != nil {
return err
}
if _, err := ctx.Exec([]string{"cp", "--archive", "node_modules", nm}, gcp.WithUserTimingAttribution); err != nil {
return err
}
}
}
if len(buildCmds) > 0 {
// If there are multiple build scripts to run, run them one-by-one so the logs are
// easier to understand.
for _, cmd := range buildCmds {
execOpts := []gcp.ExecOption{gcp.WithUserAttribution}
if nodejs.DetectSvelteKitAutoAdapter(pjs) {
execOpts = append(execOpts, gcp.WithEnv(nodejs.SvelteAdapterEnv))
}
split := strings.Split(cmd, " ")
if _, err := ctx.Exec(split, execOpts...); err != nil {
if !isCustomBuild {
return fmt.Errorf(`%w
NOTE: Running the default build script can be skipped by passing the empty environment variable "%s=" to the build`, err, nodejs.GoogleNodeRunScriptsEnv)
}
return err
}
}
shouldPrune, err := shouldPrune(ctx, pjs)
if err != nil {
return err
}
if shouldPrune {
// npm prune deletes devDependencies from node_modules
if _, err := ctx.Exec([]string{"npm", "prune", "--production"}, gcp.WithUserAttribution); err != nil {
return err
}
}
}
el, err := ctx.Layer("env", gcp.BuildLayer, gcp.LaunchLayer)
if err != nil {
return fmt.Errorf("creating layer: %w", err)
}
el.SharedEnvironment.Prepend("PATH", string(os.PathListSeparator), filepath.Join(ctx.ApplicationRoot(), "node_modules", ".bin"))
el.SharedEnvironment.Default("NODE_ENV", nodejs.NodeEnv())
// Configure the entrypoint for production.
cmd, err := nodejs.DefaultStartCommand(ctx, pjs)
if err != nil {
return fmt.Errorf("detecting start command: %w", err)
}
if !devmode.Enabled(ctx) {
ctx.AddWebProcess(cmd)
return nil
}
// Configure the entrypoint and metadata for dev mode.
if err := devmode.AddFileWatcherProcess(ctx, devmode.Config{
RunCmd: cmd,
Ext: devmode.NodeWatchedExtensions,
}); err != nil {
return fmt.Errorf("adding devmode file watcher: %w", err)
}
devmode.AddSyncMetadata(ctx, devmode.NodeSyncRules)
return nil
}
func shouldPrune(ctx *gcp.Context, pjs *nodejs.PackageJSON) (bool, error) {
// if we are vendoring dependencies, we do not need to prune
if nodejs.IsUsingVendoredDependencies() {
return false, nil
}
// if there are no devDependencies, there is no need to prune.
if !nodejs.HasDevDependencies(pjs) {
return false, nil
}
if nodeEnv := nodejs.NodeEnv(); nodeEnv != nodejs.EnvProduction {
ctx.Logf("Retaining devDependencies because $NODE_ENV=%q.", nodeEnv)
return false, nil
}
canPrune, err := nodejs.SupportsNPMPrune(ctx)
if err == nil && !canPrune {
ctx.Warnf("Retaining devDependencies because the version of NPM you are using does not support 'npm prune'.")
}
return canPrune, err
}
func upgradeNPM(ctx *gcp.Context, pjs *nodejs.PackageJSON) error {
npmVersion, err := nodejs.RequestedNPMVersion(pjs)
if err != nil {
return err
}
if npmVersion == "" {
// if an NPM version was not requested, use whatever was bundled with Node.js.
return nil
}
npmLayer, err := ctx.Layer("npm", gcp.BuildLayer, gcp.LaunchLayer, gcp.CacheLayer)
if err != nil {
return fmt.Errorf("creating layer: %w", err)
}
metaVersion := ctx.GetMetadata(npmLayer, "version")
if metaVersion == npmVersion {
ctx.Logf("npm@%s cache hit, skipping installation.", npmVersion)
return nil
}
ctx.ClearLayer(npmLayer)
prefix := fmt.Sprintf("--prefix=%s", npmLayer.Path)
pkg := fmt.Sprintf("npm@%s", npmVersion)
if _, err := ctx.Exec([]string{"npm", "install", "-g", prefix, pkg}, gcp.WithUserAttribution); err != nil {
return err
}
// Set the path here to ensure the version we just installed takes precedence over the npm bundled
// with the Node.js engine.
if err := ctx.Setenv("PATH", filepath.Join(npmLayer.Path, "bin")+":"+os.Getenv("PATH")); err != nil {
return err
}
return nil
}