GitLabgitlab-org
cli
main
1package pm
3import (
4 "encoding/json"
5 "fmt"
6 "path/filepath"
7 "runtime"
8 "slices"
9 "strings"
11 "go.yaml.in/yaml/v3"
12)
14// configSetting is one config value and where it came from: an environment
15// variable name, or a file path with the key.
16type configSetting struct {
17 source string
18 value string
19}
21// configLayers is a manager's config sources, highest precedence first. npm
22// and Yarn Classic resolve each key separately: the first layer that sets a
23// key wins, even with a value such as "" or "null" that leaves it unset.
24type configLayers []map[string]configSetting
26func (c configLayers) get(key string) (configSetting, bool) {
27 for _, layer := range c {
28 if s, ok := layer[key]; ok {
29 return s, true
30 }
31 }
32 return configSetting{}, false
33}
35// value returns the setting for key, and false when it is absent or holds a
36// value npm reads as unset.
37func (c configLayers) value(key string) (configSetting, bool) {
38 s, ok := c.get(key)
39 if !ok {
40 return configSetting{}, false
41 }
42 switch s.value {
43 case "", "false", "null", "undefined":
44 return configSetting{}, false
45 }
46 return s, true
47}
49func (s configSetting) proxy() upstreamProxy {
50 return upstreamProxy{Source: s.source, URL: s.value}
51}
53func fileSource(path, key string) string { return path + " (" + key + ")" }
55// npmUpstreamProxy picks the proxy npm uses for an HTTPS request: https-proxy
56// from any layer, then proxy from any layer, then HTTPS_PROXY. Both noproxy
57// and NO_PROXY apply (observed with npm 11.17; @npmcli/agent alone would let
58// noproxy replace NO_PROXY).
59func npmUpstreamProxy(config configLayers, environ []string) (upstreamProxy, bool) {
60 var upstream upstreamProxy
61 if s, ok := config.value("https-proxy"); ok {
62 upstream = s.proxy()
63 } else if s, ok := config.value("proxy"); ok {
64 upstream = s.proxy()
65 } else if key, value, ok := firstSetEnv(environ, true, "HTTPS_PROXY", "https_proxy"); ok {
66 upstream = upstreamProxy{Source: key, URL: value}
67 } else {
68 return upstreamProxy{}, false
69 }
70 noproxy, _ := config.value("noproxy")
71 upstream.NoProxy = joinNoProxy(noproxy.value, envNoProxy(environ))
72 return upstream, true
73}
75// readNpmConfig loads the npm config layers that can set a proxy: the
76// npm_config_* environment, the user config and the global npmrc, as
77// @npmcli/config 10 (npm 11) loads them. The project .npmrc, npm's builtin
78// npmrc, workspace roots and command-line flags are not read, and neither is
79// a user or global file inside the project.
80func readNpmConfig(environ []string, dir string) (configLayers, error) {
81 files := newConfigFiles(dir)
82 home := homeDir(environ)
83 config := configLayers{npmEnvLayer(environ)}
84 if userFile := npmUserConfigPath(config, home, dir); userFile != "" {
85 layer, err := readNpmrc(files, userFile, environ)
86 if err != nil {
87 return nil, err
88 }
89 config = append(config, layer)
90 }
91 // The user file can move the global one through prefix or globalconfig.
92 globalFile := npmGlobalConfigPath(config, environ, home, dir)
93 if globalFile == "" {
94 return config, nil
95 }
96 layer, err := readNpmrc(files, globalFile, environ)
97 if err != nil {
98 return nil, err
99 }
100 return append(config, layer), nil
101}
103// npmEnvLayer reads npm_config_* variables the way npm and Yarn Classic do:
104// the prefix in any case, the key lowercased with "_" after its first
105// character meaning "-", and empty values ignored. The last duplicate wins.
106func npmEnvLayer(environ []string) map[string]configSetting {
107 const prefix = "npm_config_"
108 layer := map[string]configSetting{}
109 for _, e := range environ {
110 k, v, ok := strings.Cut(e, "=")
111 if !ok || v == "" || len(k) <= len(prefix) || !strings.EqualFold(k[:len(prefix)], prefix) {
112 continue
113 }
114 key := k[len(prefix):]
115 if !strings.HasPrefix(key, "//") {
116 key = strings.ToLower(key[:1] + strings.ReplaceAll(key[1:], "_", "-"))
117 }
118 value, _ := npmEnvReplace(strings.TrimSpace(v), environ)
119 layer[key] = configSetting{source: k, value: value}
120 }
121 return layer
122}
124func npmUserConfigPath(config configLayers, home, dir string) string {
125 if s, ok := config.get("userconfig"); ok {
126 return npmPath(s.value, home, dir)
127 }
128 if home == "" {
129 return ""
130 }
131 return filepath.Join(home, ".npmrc")
132}
134// npmGlobalConfigPath returns globalconfig, else prefix/etc/npmrc.
135func npmGlobalConfigPath(config configLayers, environ []string, home, dir string) string {
136 if s, ok := config.get("globalconfig"); ok {
137 return npmPath(s.value, home, dir)
138 }
139 prefix := npmDefaultPrefix(environ)
140 if s, ok := config.get("prefix"); ok {
141 prefix = npmPath(s.value, home, dir)
142 }
143 if prefix == "" {
144 return ""
145 }
146 return filepath.Join(prefix, "etc", "npmrc")
147}
149// npmDefaultPrefix returns $PREFIX, else the prefix of the node binary npm
150// runs under: the directory above its bin directory, or its own directory on
151// Windows. Yarn Classic uses the same default.
152func npmDefaultPrefix(environ []string) string {
153 if prefix := lookupEnv(environ, "PREFIX"); prefix != "" {
154 return prefix
155 }
156 node, ok := lookPathIn(environ, "node")
157 if !ok {
158 return ""
159 }
160 // process.execPath, which npm derives the prefix from, has symlinks
161 // resolved.
162 if resolved, err := filepath.EvalSymlinks(node); err == nil {
163 node = resolved
164 }
165 if runtime.GOOS == "windows" {
166 return filepath.Dir(node)
167 }
168 prefix := filepath.Dir(filepath.Dir(node))
169 if destdir := lookupEnv(environ, "DESTDIR"); destdir != "" {
170 prefix = filepath.Join(destdir, prefix)
171 }
172 return prefix
173}
175// npmPath resolves a path-typed npm setting: a leading ~/ is the home
176// directory, and a relative path is relative to the working directory.
177func npmPath(value, home, dir string) string {
178 if home != "" && (strings.HasPrefix(value, "~/") || (runtime.GOOS == "windows" && strings.HasPrefix(value, `~\`))) {
179 return filepath.Join(home, value[2:])
180 }
181 if filepath.IsAbs(value) {
182 return filepath.Clean(value)
183 }
184 return filepath.Join(dir, value)
185}
187// readNpmrc reads an npmrc file as one config layer, expanding ${VAR} as npm
188// does. A missing file, or one inside the project, is an empty layer.
189func readNpmrc(files configFiles, path string, environ []string) (map[string]configSetting, error) {
190 data, ok, err := files.read(path)
191 if !ok {
192 return nil, err
193 }
194 values := parseIni(data)
195 layer := make(map[string]configSetting, len(values))
196 for k, v := range values {
197 value, _ := npmEnvReplace(v, environ)
198 layer[k] = configSetting{source: fileSource(path, k), value: value}
199 }
200 return layer, nil
201}
203// yarnIsBerry reports whether yarn in dir runs Yarn 2 or later: the nearest
204// package.json with a packageManager field pins yarn@2 or later, as Corepack
205// reads it, or a .yarnrc.yml from dir up sets yarnPath, which Classic hands
206// the run to. Otherwise the yarn on PATH is taken to be Classic, the version
207// Corepack and the yarn npm package install by default. These project files
208// only pick which rules apply, never a proxy. A yarn 1 on PATH without
209// Corepack ignores packageManager and still runs Classic, which glab then
210// treats as Berry.
211func yarnIsBerry(dir string) (bool, error) {
212 major, err := yarnPackageManagerMajor(dir)
213 if err != nil || major != 0 {
214 return major >= 2, err
215 }
216 for p := range ancestors(dir) {
217 var rc struct {
218 YarnPath string `yaml:"yarnPath"`
219 }
220 path := filepath.Join(p, ".yarnrc.yml")
221 data, found, err := readConfigFile(path)
222 if err != nil {
223 return false, err
224 }
225 if !found {
226 continue
227 }
228 if err := yaml.Unmarshal(data, &rc); err != nil {
229 return false, parseError(path, err)
230 }
231 if rc.YarnPath != "" {
232 return true, nil
233 }
234 }
235 return false, nil
236}
238// yarnPackageManagerMajor returns the yarn major version pinned by the
239// "packageManager" field of the nearest package.json that has one, from dir
240// up, skipping node_modules as Corepack does. It returns 0 when none pins
241// yarn.
242func yarnPackageManagerMajor(dir string) (int, error) {
243 for p := range ancestors(dir) {
244 if filepath.Base(p) == "node_modules" {
245 continue
246 }
247 path := filepath.Join(p, "package.json")
248 data, ok, err := readConfigFile(path)
249 if err != nil {
250 return 0, err
251 }
252 if !ok {
253 continue
254 }
255 var manifest struct {
256 PackageManager string `json:"packageManager"`
257 }
258 if err := json.Unmarshal(data, &manifest); err != nil {
259 return 0, jsonParseError(path, data, err)
260 }
261 if manifest.PackageManager == "" {
262 continue
263 }
264 version, isYarn := strings.CutPrefix(manifest.PackageManager, "yarn@")
265 if !isYarn {
266 return 0, nil
267 }
268 major := 0
269 for _, c := range version {
270 if c < '0' || c > '9' {
271 break
272 }
273 major = major*10 + int(c-'0')
274 }
275 return major, nil
276 }
277 return 0, nil
278}
280// berryUpstreamProxy follows Yarn 4's httpsProxy lookup: YARN_HTTPS_PROXY,
281// then ~/.yarnrc.yml. The .yarnrc.yml files from dir up are not read. An
282// empty httpsProxy means a direct connection, and Berry applies no NO_PROXY.
283// Per-host networkSettings are not read. With no httpsProxy, Berry connects
284// directly; this returns false so the inspection proxy keeps HTTPS_PROXY, in
285// case the run is Classic after all.
286func berryUpstreamProxy(environ []string, files configFiles) (upstreamProxy, bool, error) {
287 // Berry takes an empty YARN_HTTPS_PROXY as set, so it connects directly
288 // even when ~/.yarnrc.yml names a proxy (verified against Yarn 4.9.2).
289 for _, e := range slices.Backward(environ) {
290 if k, v, ok := strings.Cut(e, "="); ok && strings.EqualFold(k, "YARN_HTTPS_PROXY") {
291 return upstreamProxy{Source: k, URL: v}, true, nil
292 }
293 }
294 home := homeDir(environ)
295 if home == "" {
296 return upstreamProxy{}, false, nil
297 }
298 file := filepath.Join(home, ".yarnrc.yml")
299 data, found, err := files.read(file)
300 if err != nil || !found {
301 return upstreamProxy{}, false, err
302 }
303 var rc struct {
304 HTTPSProxy *string `yaml:"httpsProxy"`
305 }
306 if err := yaml.Unmarshal(data, &rc); err != nil {
307 return upstreamProxy{}, false, parseError(file, err)
308 }
309 if rc.HTTPSProxy == nil {
310 return upstreamProxy{}, false, nil
311 }
312 value, err := shellEnvReplace(*rc.HTTPSProxy, environ)
313 if err != nil {
314 return upstreamProxy{}, false, fmt.Errorf("httpsProxy in %s: %w", file, err)
315 }
316 return upstreamProxy{Source: fileSource(file, "httpsProxy"), URL: value}, true, nil
317}
319// yarnClassicUpstreamProxy follows Yarn 1.22 for HTTPS requests: https-proxy,
320// then proxy. An https-proxy of false means a direct connection. Classic
321// applies no NO_PROXY to a configured proxy; with none it falls back to
322// HTTPS_PROXY, which the inspection proxy uses by default.
323func yarnClassicUpstreamProxy(environ []string, files configFiles) (upstreamProxy, bool, error) {
324 config, err := readYarnClassicConfig(environ, files)
325 if err != nil {
326 return upstreamProxy{}, false, err
327 }
328 if s, ok := config.get("https-proxy"); ok && s.value == "false" {
329 return upstreamProxy{Source: s.source}, true, nil
330 }
331 if s, ok := config.value("https-proxy"); ok {
332 return s.proxy(), true, nil
333 }
334 s, ok := config.value("proxy")
335 return s.proxy(), ok, nil
336}
338// yarnClassicPlainHTTPProxy follows Yarn 1.22 for plain-HTTP requests, which
339// use only proxy and, without it, HTTP_PROXY.
340func yarnClassicPlainHTTPProxy(environ []string, files configFiles) (upstreamProxy, bool, error) {
341 config, err := readYarnClassicConfig(environ, files)
342 if err != nil {
343 return upstreamProxy{}, false, err
344 }
345 s, ok := config.value("proxy")
346 return s.proxy(), ok, nil
347}
349// readYarnClassicConfig loads the config layers Yarn 1.22 reads a proxy from,
350// highest precedence first: its own (YARN_HTTPS_PROXY and YARN_PROXY, the user
351// .yarnrc, then <prefix>/etc/yarnrc), then npm's (npm_config_*, the user
352// .npmrc, then <prefix>/etc/npmrc), which Classic consults only for a key its
353// own config leaves undefined. The .yarnrc and .npmrc files of the project and
354// its ancestors are not read, and neither is a user or global file inside the
355// project.
356func readYarnClassicConfig(environ []string, files configFiles) (configLayers, error) {
357 home := homeDir(environ)
358 // Classic matches these variable names case-insensitively.
359 _, yarnUser, _ := firstSetEnv(environ, true, "YARN_USERCONFIG")
360 _, npmUser, _ := firstSetEnv(environ, true, "NPM_CONFIG_USERCONFIG")
361 if npmUser == "" {
362 npmUser = yarnUser
363 }
364 if home != "" {
365 if yarnUser == "" {
366 yarnUser = filepath.Join(home, ".yarnrc")
367 }
368 if npmUser == "" {
369 npmUser = filepath.Join(home, ".npmrc")
370 }
371 }
372 var yarnGlobal, npmGlobal string
373 if prefix := npmDefaultPrefix(environ); prefix != "" {
374 yarnGlobal, npmGlobal = filepath.Join(prefix, "etc", "yarnrc"), filepath.Join(prefix, "etc", "npmrc")
375 }
377 yarnEnv := map[string]configSetting{}
378 for key, name := range map[string]string{"https-proxy": "YARN_HTTPS_PROXY", "proxy": "YARN_PROXY"} {
379 if k, v, ok := firstSetEnv(environ, true, name); ok {
380 yarnEnv[key] = configSetting{source: k, value: v}
381 }
382 }
383 config := configLayers{yarnEnv}
384 for _, file := range []string{yarnUser, yarnGlobal} {
385 if file == "" {
386 continue
387 }
388 layer, err := readYarnrc(files, file)
389 if err != nil {
390 return nil, err
391 }
392 config = append(config, layer)
393 }
395 config = append(config, npmEnvLayer(environ))
396 for _, file := range []string{npmUser, npmGlobal} {
397 if file == "" {
398 continue
399 }
400 data, ok, err := files.read(file)
401 if err != nil {
402 return nil, err
403 }
404 if !ok {
405 continue
406 }
407 values := parseIni(data)
408 layer := make(map[string]configSetting, len(values))
409 for k, v := range values {
410 value, missing := npmEnvReplace(v, environ)
411 if missing {
412 return nil, fmt.Errorf("failed to read %s: %w", file, errUnsetEnv)
413 }
414 layer[k] = configSetting{source: fileSource(file, k), value: value}
415 }
416 config = append(config, layer)
417 }
418 return config, nil
419}
Diagnostics7 git calls · 236 ms
CallStatusSizeTime0236 ms
discover200185 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=185 git.server.agent=git/2.56.g1891de4-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=185 git.pkt.payload.max=35
ls-refs20058 KiB2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=59849 git.response.cache=UPDATING git.response.duration_ms=2 git.protocol.version=2 git.pkt.frames=731 git.pkt.data=730 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=59849 git.pkt.payload.max=143 git.ls_refs.count=730 git.ls_refs.head=refs/heads/main
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=combine:blob:none+tree:2 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=1 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=4273 git.pkt.payload.max=4173 git.sideband.band1.bytes=4173 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=11 git.pack.bytes=4173 git.fetch.wide.objects=11 git.fetch.wide.extra_bytes=3930 git.fetch.wide.blobs=0
fetchtree:0 · deepen 1020032 KiB · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=32926 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=22 git.pkt.data=20 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=32926 git.pkt.payload.max=32046 git.sideband.band1.bytes=32046 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=80 git.pack.bytes=32046 git.pack.ofs_delta=36 git.pack.ref_delta=0 git.pack.chain.max=2 git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91 git.pack.full=44Open pack map
fetchtree:0200361 B · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=361 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=506cba622ceda1473ee3cde63969809c144b7247 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=361 git.pkt.payload.max=334 git.sideband.band1.bytes=334 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=334 git.pack.trailer=26d32585a3a474717e00a318584ccd16918c9d32Open pack map
fetchtree:02001.3 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=1327 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=2758711b27b3e124482ce287b5df86092bb20d62 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1327 git.pkt.payload.max=1300 git.sideband.band1.bytes=1300 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1300 git.pack.trailer=c83fc6004a9a1360845513c0fb16272b5c14a3e4Open pack map
fetch2004.1 KiB227 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=227 git.response.bytes=4238 git.response.cache=MISS git.response.duration_ms=227 git.fetch.wants=56b57ad92491706de1fd7a3d85495476db5423e2 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=4238 git.pkt.payload.max=4211 git.sideband.band1.bytes=4211 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://gitlab.com/gitlab-org/cli
- Ref
- refs/heads/main
- Commit
- 0dd7574dce9e385c5c30540e6c15657adc6bdce9
- cf-ray
- a5e256d18d271ae0
- Request
- 1791e512…
- Protocol
- v2 · sha1
- Agent
- git/2.56.g1891de4-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format