Skip to content

Commit f43e0db

Browse files
committed
HOTFIX: User config should be copied before overriding (#3015)
* User config should be copied before overriding * should override userconfig only if auth helper is being run * string changes * Add npm auth helper 23.0 (disabled fallback cred provider)
1 parent b6899c6 commit f43e0db

File tree

6 files changed

+48
-15
lines changed

6 files changed

+48
-15
lines changed

Tasks/Npm/Strings/resources.resjson/en-US/resources.resjson

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
"loc.helpMarkDown": "[More Information](https://go.microsoft.com/fwlink/?LinkID=613746)",
44
"loc.description": "Run a npm command",
55
"loc.instanceNameFormat": "npm $(command)",
6-
"loc.input.label.cwd": "Working directory",
6+
"loc.input.label.cwd": "working folder",
77
"loc.input.help.cwd": "Working directory where the npm command is run. Defaults to the root of the repo.",
8-
"loc.input.label.command": "npm command to execute",
9-
"loc.input.label.arguments": "Arguments",
8+
"loc.input.label.command": "npm command",
9+
"loc.input.label.arguments": "arguments",
1010
"loc.input.help.arguments": "Additional arguments passed to npm.",
1111
"loc.messages.InvalidCommand": "Only one command should be used for npm. Use 'Arguments' input for additional arguments.",
1212
"loc.messages.NpmReturnCode": "npm exited with return code: %d",

Tasks/Npm/Tests/NpmMockHelper.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,15 @@ export class NpmMockHelper {
111111
private setDefaultAnswers() {
112112
this.setToolPath(this.answers, "npm", NpmMockHelper.NpmCmdPath);
113113
this.setOsType('WiNdOWs_nT');
114+
this.setProjectNpmrcExists();
114115
}
115116

116117
private setToolPath(answers: ma.TaskLibAnswers, tool: string, path: string) {
117118
answers.which[tool] = path;
118119
answers.checkPath[path] = true;
119120
}
121+
122+
private setProjectNpmrcExists() {
123+
this.answers.exist[path.join(NpmMockHelper.FakeWorkingDirectory, '.npmrc')] = true;
124+
}
120125
}

Tasks/Npm/make.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"externals": {
33
"archivePackages": [
44
{
5-
"url": "https://vstsagenttools.blob.core.windows.net/tools/vsts-npm-auth/0.21.0/vsts-npm-auth.zip",
5+
"url": "https://vstsagenttools.blob.core.windows.net/tools/vsts-npm-auth/0.23.0/vsts-npm-auth.zip",
66
"dest": "./Npm/vsts-npm-auth/"
77
},
88
{

Tasks/Npm/npmtask.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,28 @@ async function executeTask() {
4242
} else {
4343

4444
// new task version with auth support
45-
var npmrcPath: string = path.join(cwd, '.npmrc');
46-
var tempNpmrcPath : string = getTempNpmrcPath();
47-
var debugLog: boolean = tl.getVariable('system.debug') && tl.getVariable('system.debug').toLowerCase() === 'true';
48-
4945
try{
50-
if(tl.osType().toLowerCase() === 'windows_nt') {
46+
47+
var npmrcPath: string = path.join(cwd, '.npmrc');
48+
var tempNpmrcPath : string = getTempNpmrcPath();
49+
50+
var debugLog: boolean = tl.getVariable('system.debug') && tl.getVariable('system.debug').toLowerCase() === 'true';
51+
52+
var shouldRunAuthHelper: boolean = tl.osType().toLowerCase() === 'windows_nt' && tl.exist(npmrcPath);
53+
if(shouldRunAuthHelper) {
54+
copyUserNpmrc(tempNpmrcPath);
5155
await runNpmAuthHelperAsync(getNpmAuthHelperRunner(npmrcPath, tempNpmrcPath, debugLog));
5256
}
5357

5458
// set required environment variables for npm execution
5559
var npmExecOptions = <trm.IExecOptions>{
5660
env: extend({}, process.env)
5761
};
58-
npmExecOptions.env['npm_config_userconfig'] = tempNpmrcPath;
62+
63+
if(shouldRunAuthHelper){
64+
npmExecOptions.env['npm_config_userconfig'] = tempNpmrcPath;
65+
}
66+
5967
if(debugLog) {
6068
npmExecOptions.env['npm_config_loglevel'] = 'verbose';
6169
}
@@ -87,6 +95,25 @@ async function runNpmAuthHelperAsync(npmAuthRunner: trm.ToolRunner) : Promise<nu
8795
}
8896
}
8997

98+
function copyUserNpmrc(tempNpmrcPath: string) {
99+
// copy the user level npmrc contents, if it exists.
100+
var currentUserNpmrcPath : string = getUserNpmrcPath();
101+
if(tl.exist(currentUserNpmrcPath)) {
102+
tl.debug(`Copying ${currentUserNpmrcPath} to ${tempNpmrcPath} ...`);
103+
tl.cp(currentUserNpmrcPath, tempNpmrcPath, /* options */ null, /* continueOnError */ true);
104+
}
105+
}
106+
107+
function getUserNpmrcPath() {
108+
var userNpmRc = process.env['npm_config_userconfig'];
109+
if(!userNpmRc){
110+
// default npm rc is located at user's home folder.
111+
userNpmRc = path.join(process.env['HOMEDRIVE'], process.env['HOMEPATH'], '.npmrc');
112+
}
113+
tl.debug(`User npm rc: ${userNpmRc}`);
114+
return userNpmRc;
115+
}
116+
90117
async function tryRunNpmConfigAsync(npmConfigRunner: trm.ToolRunner, execOptions : trm.IExecOptions) {
91118
try {
92119
var code = await npmConfigRunner.exec(execOptions);
@@ -178,6 +205,7 @@ function addBuildCredProviderEnv(env: EnvironmentDictionary) : EnvironmentDictio
178205
env['VSS_NUGET_ACCESSTOKEN'] = accessToken;
179206
env['VSS_NUGET_URI_PREFIXES'] = urlPrefixes.join(';');
180207
env['NPM_CREDENTIALPROVIDERS_PATH'] = credProviderPath;
208+
env['VSS_DISABLE_DEFAULTCREDENTIALPROVIDER'] = '1';
181209
return env;
182210
}
183211

Tasks/Npm/task.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"version": {
1010
"Major": 0,
1111
"Minor": 2,
12-
"Patch": 17
12+
"Patch": 18
1313
},
1414
"demands": [
1515
"npm"
@@ -20,22 +20,22 @@
2020
{
2121
"name": "cwd",
2222
"type": "filePath",
23-
"label": "Working directory",
23+
"label": "working folder",
2424
"defaultValue": "",
2525
"required": false,
2626
"helpMarkDown": "Working directory where the npm command is run. Defaults to the root of the repo."
2727
},
2828
{
2929
"name": "command",
3030
"type": "string",
31-
"label": "npm command to execute",
31+
"label": "npm command",
3232
"defaultValue": "install",
3333
"required": true
3434
},
3535
{
3636
"name": "arguments",
3737
"type": "string",
38-
"label": "Arguments",
38+
"label": "arguments",
3939
"defaultValue": "",
4040
"helpMarkDown": "Additional arguments passed to npm.",
4141
"required": false

Tasks/Npm/task.loc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"version": {
1010
"Major": 0,
1111
"Minor": 2,
12-
"Patch": 17
12+
"Patch": 18
1313
},
1414
"demands": [
1515
"npm"

0 commit comments

Comments
 (0)