Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Use NPMRC_DATA to write .npmrc file in auto-indexing job for Typescript #53529

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/code_navigation/how-to/configure_auto_indexing.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,7 @@ Under the hood, this information will be used to write the [.netrc](https://www.
### TypeScript/JavaScript

For **NPM**, you can create a [secret named `NPM_TOKEN`](https://docs.npmjs.com/using-private-packages-in-a-ci-cd-workflow#set-the-token-as-an-environment-variable-on-the-cicd-server) which will be automatically picked up by the indexer.

If you are using something like [JFrog Artifactory for self-hosted NPM registry](https://jfrog.com/help/r/jfrog-artifactory-documentation/npm-registry), the preferred method of authentication is an `.npmrc` file. If you configure a secret named `NPMRC_DATA`, auto-indexing executor will write the contents of that secret into a `~/.nprmc` file, which should be recognised by tools like NPM and Yarn when connecting to your private NPM registry.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which should be recognised by tools like NPM and Yarn

Are you sure that Yarn supports this? Looking at the comments here: yarnpkg/yarn#871 as well as this SO answer: https://stackoverflow.com/a/72365311/2682729 -- Yarn seems to require a different .yarnrc file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm reading the PR you linked it falls back to .npmrc if it can't find its own file?

Also JFrog suggest that Yarn explicitly works: https://jfrog.com/help/r/jfrog-artifactory-documentation/using-yarn

The way I'm reading it is that you can use either, and Yarn obviously prefers its own thing, but it should support whatever is in .npmrc as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JFrog's docs suggest that auth information in .npmrc should work
The issue you mentioned suggests that the registry address itself only works from .yarnc

Looks a bit messy, but may be we should get this into the hands of customers and see if it works for them? If not can always add YARNRC_DATA for this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm reading the PR you linked it falls back to .npmrc if it can't find its own file?

Right, but some commenters point out that it doesn't work, at least in some situations (e.g. this comment: yarnpkg/yarn#871 (comment)). I can't figure out the details though, whether it's "just" user error or not...

JFrog's docs suggest that auth information in .npmrc should work

Could you add a link to the docs in the Lua code where the .npmrc is being written, indicating that it is meant to work for Yarn too?

Looks a bit messy, but may be we should get this into the hands of customers and see if it works for them? If not can always add YARNRC_DATA for this.

It seems like the data is the same, the only difference is the format, right? So would it make sense to unconditionally write the .yarnrc file too, just in case?

Also, in terms of a feedback loop, I think it will not be fun to debug if a customer runs into this... If we'd like to merge this soon because of code freeze, I'm OK with merging this...

FWIW, in the past, I've used the free Artifactory trial to actually test out this kind of situation. It's pretty cumbersome, but it gave greater confidence in things working as expected. And as cumbersome as it was, it was still faster than iterating with a customer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like the data is the same, the only difference is the format, right? So would it make sense to unconditionally write the .yarnrc file too, just in case?

Ah, I see what you mean now.

Took a look at the docs for Yarn itself - seems like we can't get away with same data:

Starting from the v2, they must be written in valid Yaml and have the right extension (simply calling your file .yarnrc won't do).

I will amend the docs to only mention npm, and address Yarn separately (there's an issue with Executors UI I need to fix beforehand)


For the information on contents of this file please refer to official [NPM](https://docs.npmjs.com/cli/v9/configuring-npm/npmrc?v=true#auth-related-configuration) documentation.
varungandhi-src marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ import (

func TestTypeScriptGenerator(t *testing.T) {
expectedIndexerImage, _ := libs.DefaultIndexerForLang("typescript")
npmrcStep := `if [ "$NPMRC_DATA" ]; then
echo "Writing npmrc config to $HOME/.npmrc"
echo "$NPMRC_DATA" > ~/.npmrc
else
echo "No npmrc config set, continuing"
fi`
nodeMemoryStep := `if [ -n "${VM_MEM_MB:-}" ]; then export NODE_OPTIONS="--max-old-space-size=$VM_MEM_MB"; fi`

expectedLocalSteps := []string{nodeMemoryStep, npmrcStep}

testGenerators(t,
generatorTestCase{
Expand All @@ -25,7 +34,7 @@ func TestTypeScriptGenerator(t *testing.T) {
Commands: []string{"npm install --ignore-scripts"},
},
},
LocalSteps: []string{`if [ -n "${VM_MEM_MB:-}" ]; then export NODE_OPTIONS="--max-old-space-size=$VM_MEM_MB"; fi`},
LocalSteps: expectedLocalSteps,
Root: "",
Indexer: expectedIndexerImage,
IndexerArgs: []string{"scip-typescript", "index", "--infer-tsconfig"},
Expand All @@ -49,7 +58,7 @@ func TestTypeScriptGenerator(t *testing.T) {
Commands: []string{"yarn --ignore-scripts"},
},
},
LocalSteps: []string{`if [ -n "${VM_MEM_MB:-}" ]; then export NODE_OPTIONS="--max-old-space-size=$VM_MEM_MB"; fi`},
LocalSteps: expectedLocalSteps,
Root: "",
Indexer: expectedIndexerImage,
IndexerArgs: []string{"scip-typescript", "index", "--infer-tsconfig"},
Expand All @@ -66,7 +75,7 @@ func TestTypeScriptGenerator(t *testing.T) {
expected: []config.IndexJob{
{
Steps: nil,
LocalSteps: []string{`if [ -n "${VM_MEM_MB:-}" ]; then export NODE_OPTIONS="--max-old-space-size=$VM_MEM_MB"; fi`},
LocalSteps: expectedLocalSteps,
Root: "",
Indexer: expectedIndexerImage,
IndexerArgs: []string{"scip-typescript", "index"},
Expand All @@ -85,7 +94,7 @@ func TestTypeScriptGenerator(t *testing.T) {
expected: []config.IndexJob{
{
Steps: nil,
LocalSteps: []string{`if [ -n "${VM_MEM_MB:-}" ]; then export NODE_OPTIONS="--max-old-space-size=$VM_MEM_MB"; fi`},
LocalSteps: expectedLocalSteps,
Root: "a",
Indexer: expectedIndexerImage,
IndexerArgs: []string{"scip-typescript", "index"},
Expand All @@ -94,7 +103,7 @@ func TestTypeScriptGenerator(t *testing.T) {
},
{
Steps: nil,
LocalSteps: []string{`if [ -n "${VM_MEM_MB:-}" ]; then export NODE_OPTIONS="--max-old-space-size=$VM_MEM_MB"; fi`},
LocalSteps: expectedLocalSteps,
Root: "b",
Indexer: expectedIndexerImage,
IndexerArgs: []string{"scip-typescript", "index"},
Expand All @@ -103,7 +112,7 @@ func TestTypeScriptGenerator(t *testing.T) {
},
{
Steps: nil,
LocalSteps: []string{`if [ -n "${VM_MEM_MB:-}" ]; then export NODE_OPTIONS="--max-old-space-size=$VM_MEM_MB"; fi`},
LocalSteps: expectedLocalSteps,
Root: "c",
Indexer: expectedIndexerImage,
IndexerArgs: []string{"scip-typescript", "index"},
Expand Down Expand Up @@ -133,7 +142,7 @@ func TestTypeScriptGenerator(t *testing.T) {
Commands: []string{"npm install"},
},
},
LocalSteps: []string{`if [ -n "${VM_MEM_MB:-}" ]; then export NODE_OPTIONS="--max-old-space-size=$VM_MEM_MB"; fi`},
LocalSteps: expectedLocalSteps,
Root: "",
Indexer: expectedIndexerImage,
IndexerArgs: []string{"scip-typescript", "index"},
Expand All @@ -153,7 +162,7 @@ func TestTypeScriptGenerator(t *testing.T) {
Commands: []string{"yarn"},
},
},
LocalSteps: []string{`if [ -n "${VM_MEM_MB:-}" ]; then export NODE_OPTIONS="--max-old-space-size=$VM_MEM_MB"; fi`},
LocalSteps: expectedLocalSteps,
Root: "foo/bar/baz",
Indexer: expectedIndexerImage,
IndexerArgs: []string{"scip-typescript", "index"},
Expand All @@ -178,7 +187,7 @@ func TestTypeScriptGenerator(t *testing.T) {
Commands: []string{"npm install"},
},
},
LocalSteps: []string{`if [ -n "${VM_MEM_MB:-}" ]; then export NODE_OPTIONS="--max-old-space-size=$VM_MEM_MB"; fi`},
LocalSteps: expectedLocalSteps,
Root: "foo/bar/bonk",
Indexer: expectedIndexerImage,
IndexerArgs: []string{"scip-typescript", "index"},
Expand All @@ -193,7 +202,7 @@ func TestTypeScriptGenerator(t *testing.T) {
Commands: []string{"npm install"},
},
},
LocalSteps: []string{`if [ -n "${VM_MEM_MB:-}" ]; then export NODE_OPTIONS="--max-old-space-size=$VM_MEM_MB"; fi`},
LocalSteps: expectedLocalSteps,
Root: "foo/baz",
Indexer: expectedIndexerImage,
IndexerArgs: []string{"scip-typescript", "index"},
Expand All @@ -218,7 +227,7 @@ func TestTypeScriptGenerator(t *testing.T) {
Commands: []string{"yarn"},
},
},
LocalSteps: []string{`if [ -n "${VM_MEM_MB:-}" ]; then export NODE_OPTIONS="--max-old-space-size=$VM_MEM_MB"; fi`},
LocalSteps: expectedLocalSteps,
Root: "",
Indexer: expectedIndexerImage,
IndexerArgs: []string{"scip-typescript", "index"},
Expand All @@ -243,10 +252,10 @@ func TestTypeScriptGenerator(t *testing.T) {
Commands: []string{"N_NODE_MIRROR=https://unofficial-builds.nodejs.org/download/release n --arch x64-musl auto", "npm install"},
},
},
LocalSteps: []string{
"N_NODE_MIRROR=https://unofficial-builds.nodejs.org/download/release n --arch x64-musl auto",
`if [ -n "${VM_MEM_MB:-}" ]; then export NODE_OPTIONS="--max-old-space-size=$VM_MEM_MB"; fi`,
},
LocalSteps: append(
[]string{"N_NODE_MIRROR=https://unofficial-builds.nodejs.org/download/release n --arch x64-musl auto"},
expectedLocalSteps...,
),
Root: "",
Indexer: expectedIndexerImage,
IndexerArgs: []string{"scip-typescript", "index"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,27 @@ local util = require("sg.autoindex.util")

local indexer = require("sg.autoindex.indexes").get "typescript"
local typescript_nmusl_command =
"N_NODE_MIRROR=https://unofficial-builds.nodejs.org/download/release n --arch x64-musl auto"
"N_NODE_MIRROR=https://unofficial-builds.nodejs.org/download/release n --arch x64-musl auto"

local exclude_paths = pattern.new_path_combine(shared.exclude_paths, {
pattern.new_path_segment("node_modules"),
})

local npmrc_steps = [[if [ "$NPMRC_DATA" ]; then
echo "Writing npmrc config to $HOME/.npmrc"
echo "$NPMRC_DATA" > ~/.npmrc
else
echo "No npmrc config set, continuing"
fi]]



local safe_decode = function(encoded)
local _, payload = pcall(function()
return json.decode(encoded)
end)
local _, payload = pcall(function()
return json.decode(encoded)
end)

return payload
return payload
end

local check_lerna_file = function(root, contents_by_path)
Expand Down Expand Up @@ -125,6 +134,8 @@ local infer_typescript_job = function(api, tsconfig_path, should_infer_config)
'if [ -n "${VM_MEM_MB:-}" ]; then export NODE_OPTIONS="--max-old-space-size=$VM_MEM_MB"; fi'
)

table.insert(local_steps, npmrc_steps)

varungandhi-src marked this conversation as resolved.
Show resolved Hide resolved
local args = { "scip-typescript", "index" }
if should_infer_config then
table.insert(args, "--infer-tsconfig")
Expand All @@ -137,7 +148,7 @@ local infer_typescript_job = function(api, tsconfig_path, should_infer_config)
indexer = indexer,
indexer_args = args,
outfile = "index.scip",
requested_envvars = { "NPM_TOKEN" },
requested_envvars = { "NPM_TOKEN", "NPMRC_DATA" },
}
end,
}))
Expand Down