-
Notifications
You must be signed in to change notification settings - Fork 9
/
build.go
164 lines (135 loc) · 4.8 KB
/
build.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package nodejs
import (
"context"
"fmt"
"os"
"strings"
"github.com/moby/buildkit/client/llb"
specs "github.com/opencontainers/image-spec/specs-go/v1"
"namespacelabs.dev/foundation/internal/build"
"namespacelabs.dev/foundation/internal/build/buildkit"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/fnfs"
"namespacelabs.dev/foundation/internal/fnfs/memfs"
"namespacelabs.dev/foundation/internal/integrations/nodejs/binary"
"namespacelabs.dev/foundation/internal/integrations/nodejs/yarnplugin"
"namespacelabs.dev/foundation/internal/llbutil"
"namespacelabs.dev/foundation/internal/sdk/yarn"
"namespacelabs.dev/foundation/schema"
)
const (
yarnLockFilePath = "/fn.lock.json"
YarnContainerCacheDir = "/cache/yarn"
yarnRcContent = `nodeLinker: node-modules
enableTelemetry: false
logFilters:
- code: YN0013
level: discard
- code: YN0072
level: discard
`
// All dependencies that are not from the same module copied here. This includes
// dependencies used as "Dep" in workspace (copied from the Namespace cache)
// and the ones used as "Replace" (copied from the user's file system).
// TODO: figure out why tsc fails if it is not under "/app"
DepsRootPath = "/app/external_deps"
)
func RunYarnShlex(args ...string) llb.RunOption {
return llb.Shlex(fmt.Sprintf("node %s %s", yarnBinaryPath, strings.Join(args, " ")))
}
func prepareNodejsBaseWithYarn(ctx context.Context, nodejsBase string, platform specs.Platform) (llb.State, error) {
base := llbutil.Image(nodejsBase, platform)
buildBase := base.Run(llb.Shlex("apk add --no-cache python2 make g++")).Root()
buildBase, err := copyYarnBinaryFromCache(ctx, buildBase)
if err != nil {
return llb.State{}, err
}
buildBase, err = generateYarnAuxFiles(ctx, buildBase)
if err != nil {
return llb.State{}, err
}
return buildBase, nil
}
func copyYarnBinaryFromCache(ctx context.Context, base llb.State) (llb.State, error) {
// TODO: feed Yarn SDK as a dependency to the graph to speed up the initial build.
yarnBin, err := yarn.EnsureSDK(ctx)
if err != nil {
return llb.State{}, err
}
yarnBinContent, err := os.ReadFile(string(yarnBin))
if err != nil {
return llb.State{}, err
}
var fsys memfs.FS
fsys.Add(yarnBinaryPath, yarnBinContent)
state, err := llbutil.WriteFS(ctx, &fsys, base, ".")
if err != nil {
return llb.State{}, err
}
return state, nil
}
func writeYarnAuxFiles(ctx context.Context, out fnfs.WriteFS) error {
if err := fnfs.WriteFile(ctx, out, pluginFn, yarnplugin.PluginContent(), 0644); err != nil {
return err
}
if err := fnfs.WriteFile(ctx, out, yarnRcFn, []byte(yarnRcContent), 0644); err != nil {
return err
}
return nil
}
func generateYarnAuxFiles(ctx context.Context, base llb.State) (llb.State, error) {
var fsys memfs.FS
if err := writeYarnAuxFiles(ctx, &fsys); err != nil {
return llb.State{}, err
}
state, err := llbutil.WriteFS(ctx, &fsys, base, ".")
if err != nil {
return llb.State{}, err
}
return state, nil
}
func PrepareNodejsBaseWithYarnForBuild(ctx context.Context, nodejsBase string, platform specs.Platform) (llb.State, error) {
buildBase, err := prepareNodejsBaseWithYarn(ctx, nodejsBase, platform)
if err != nil {
return llb.State{}, err
}
buildBase = buildBase.
AddEnv("YARN_CACHE_FOLDER", YarnContainerCacheDir).
AddEnv(fnYarnLockEnvVar, yarnLockFilePath)
for k, v := range yarnEnvArgs("/") {
buildBase = buildBase.AddEnv(k, v)
}
buildBase = buildBase.AddEnv(fnYarnLockEnvVar, yarnLockFilePath)
return buildBase, nil
}
func AddExternalModules(ctx context.Context, workspace *schema.Workspace, rel string, base llb.State, externalModules []build.Workspace) ([]buildkit.LocalContents, llb.State, error) {
locals := []buildkit.LocalContents{}
lockFileStruct, err := generateLockFileStructForBuild(rel, workspace)
if err != nil {
return nil, llb.State{}, err
}
buildBase, err := llbutil.AddSerializedJsonAsFile(base, yarnLockFilePath, lockFileStruct)
if err != nil {
return nil, llb.State{}, err
}
for _, m := range externalModules {
// Copying external modules to "DepsRootPath".
lfModule, ok := lockFileStruct.Modules[m.ModuleName()]
if !ok {
return nil, llb.State{}, fnerrors.InternalError("module %s not found in the Namespace lock file", workspace.ModuleName)
}
moduleLocal := buildkit.LocalContents{
Module: m,
Path: ".",
ObserveChanges: false,
ExcludePatterns: binary.NodejsExclude,
}
locals = append(locals, moduleLocal)
localState := buildkit.MakeLocalState(moduleLocal)
buildBase = buildBase.With(llbutil.CopyFrom(localState, ".", lfModule.Path))
}
return locals, buildBase, nil
}