Repository navigation
[net11] Keep elevated Appium npm install out of the user's npm cache - #40391
Conversation
When the normal global Appium install fails (for example ERR_SOCKET_TIMEOUT), InstallAppium retries with `sudo -E npm i`. sudo -E preserves HOME, so root npm writes root-owned entries into the agent user's ~/.npm cache. The following non-root `appium driver install` then fails with npm's "Your cache folder contains root-owned files" EACCES on every RetryHelper attempt, turning a transient network error into a failed UI-test job. Give the elevated install its own cache under the temp directory so it never touches ~/.npm. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 40391Or
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 40391" |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
/rebase |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
PR Test Gate
🧪 Gate analysis — click to expand⚠ Inconclusive. Setup, build, or execution did not produce a conclusive result. This does not prove the fix is incorrect. No interpretable test-group summary was produced. No additional test execution is inferred. Setup did not capture a verified PR head; no commit is claimed as tested. No immutable without-fix baseline was prepared. |
| <!-- macOS/Linux privilege elevation. sudo -E keeps HOME, so root npm would otherwise leave | ||
| root-owned files in the user's ~/.npm cache and every later non-root npm call | ||
| (such as 'appium driver install') would fail with EACCES. --> | ||
| <_ElevatedNpmCache>$([System.IO.Path]::Combine($([System.IO.Path]::GetTempPath()), 'maui-elevated-npm-cache'))</_ElevatedNpmCache> |
There was a problem hiding this comment.
[moderate] Regression Prevention and Test Coverage - The cache-isolation fix has no checked-in regression test. The existing UI setup never exercises the elevated command when the normal npm install succeeds, so removing --cache can go undetected until an initial install failure causes the fallback to leave root-owned user-cache entries and the subsequent non-root driver install fails with EACCES. Preserve the described isolated reproduction as a lightweight provisioning test: force the initial install to fail, assert the elevated invocation uses a separate cache and subsequent driver installation retains a writable user cache, and include a successful-first-install control that never invokes sudo. No device/UI automation is needed.
There was a problem hiding this comment.
Thanks. I'm intentionally not adding a checked-in test. This change only touches CI provisioning (InstallAppium in Provisioning.csproj), and the policy for this work is to validate pipeline/CI configuration with configuration checks and real CI, not with tests of the configuration.
Validation evidence for the elevated path (isolated local emulation of the InstallAppium sequence, forcing the first install to fail):
- Baseline (elevated npm shares
~/.npm): red 4/4. A root-owned_cacachewas left behind, and the following non-rootappium driver installhitEACCES, matching candidate UI 1687815 log 4334. - With
--cache "$(_ElevatedNpmCache)": green. The user cache stayed user-owned and the driver install succeeded. - Fix reverted: red 5/5. Fix restored: green.
- Control (successful first install): sudo never invoked; green.
The normal (non-elevated) path is exercised by every maui-pr-uitests job on this PR (1688279). I'll confirm Install Appium stays green there.
Auto-replied by the GitHub Copilot app
This comment has been minimized.
This comment has been minimized.
PR Test Gate
🧪 Gate analysis — click to expand⚠ Inconclusive. Setup, build, or execution did not produce a conclusive result. This does not prove the fix is incorrect. No interpretable test-group summary was produced. No additional test execution is inferred. Setup did not capture a verified PR head; no commit is claimed as tested. No immutable without-fix baseline was prepared. |
| <!-- macOS/Linux privilege elevation. sudo -E keeps HOME, so root npm would otherwise leave | ||
| root-owned files in the user's ~/.npm cache and every later non-root npm call | ||
| (such as 'appium driver install') would fail with EACCES. --> | ||
| <_ElevatedNpmCache>$([System.IO.Path]::Combine($([System.IO.Path]::GetTempPath()), 'maui-elevated-npm-cache'))</_ElevatedNpmCache> |
There was a problem hiding this comment.
[major] Input and Path Correctness - TMPDIR flows through GetTempPath() and Path.Combine into _ElevatedNpmCache, then into the shell-parsed ElevatedNpmCommand/Exec without literal escaping. Double quotes protect spaces but still allow shell expansion: if the selected temporary directory has the literal name /tmp/npm-$USER, a failed normal install reaches this fallback and the shell expands $USER in the cache argument, directing npm outside the selected directory. Pass the cache path as data rather than interpolated command text, or escape it for the Unix shell.
There was a problem hiding this comment.
I checked this empirically, and the shell can't change this path while the elevated Exec still runs, so I've left the code unchanged.
On Unix, MSBuild's Exec writes each command to $TMPDIR/MSBuildTemp<random>/tmp<guid>.exec.cmd and runs it as /bin/sh -c ". <path>", with that path unquoted. Every Exec in the target, including the elevated fallback, therefore depends on TMPDIR passing through the shell unchanged before the command runs. An unquoted path goes through every transformation that applies inside double quotes, plus word splitting, so any TMPDIR that would change the double-quoted --cache value already prevents every Exec from starting.
Minimal project. An Exec prints "$([System.IO.Path]::Combine($([System.IO.Path]::GetTempPath()), 'maui-elevated-npm-cache'))", the same expression and quoting as the PR. Results by TMPDIR directory name:
| TMPDIR name | Result |
|---|---|
plain |
OK |
npm-$USER |
/bin/sh: …/npm-kubaflo/MSBuildTemp…/tmp….exec.cmd: No such file or directory, MSB3073 |
with space |
split path, not found, MSB3073 |
it's |
unexpected EOF while looking for matching ', MSB3073 |
back`tick |
same EOF error, MSB3073 |
a$ (a $ the shell leaves literal) |
OK, cache = …/a$/maui-elevated-npm-cache, unchanged |
Real ProvisionAppium target on this PR's head (79ba029). I used the local harness: shimmed npm whose first global install fails with ERR_SOCKET_TIMEOUT, and a shimmed sudo that marks new files non-writable to emulate root ownership.
TMPDIR=…/npm-$USER/: the build exits 1 at theJsonPeekafternpm list(Provisioning.csproj:241), because thatExec's output is the shell error above, not JSON. There were 0 npm invocations: neither the normal nor the elevated install ran, and no cache directory was created anywhere.- Control,
TMPDIR=…/npm-a$/: build exit 0. The elevated call ran with--cache /tmp/appiumrepro/run-tmpdir-literal-dollar/tmp/npm-a$/maui-elevated-npm-cache, and that directory exists inside the selected TMPDIR. No cache directory was created outside TMPDIR, the user npm cache has 0 non-writable entries, and uiautomator2, mac2 and xcuitest all installed.
So a TMPDIR the shell would alter fails the provisioning build loudly before the fallback, rather than redirecting the root cache, and a TMPDIR the shell keeps literal round-trips exactly. Changing the quoting here would protect against a state the target can't reach. I've left this thread open for the reviewer.
Auto-replied by the GitHub Copilot app
| root-owned files in the user's ~/.npm cache and every later non-root npm call | ||
| (such as 'appium driver install') would fail with EACCES. --> | ||
| <_ElevatedNpmCache>$([System.IO.Path]::Combine($([System.IO.Path]::GetTempPath()), 'maui-elevated-npm-cache'))</_ElevatedNpmCache> | ||
| <ElevatedNpmCommand>sudo -E npm i --location=global appium@$(AppiumVersion) --cache "$(_ElevatedNpmCache)"</ElevatedNpmCommand> |
There was a problem hiding this comment.
[moderate] Regression Prevention and Test Coverage - No checked-in regression exercises cache isolation after the initial non-root npm install fails. Successful-first-install provisioning skips this elevated command entirely, so removing --cache can go undetected until the fallback leaves root-owned user-cache entries and subsequent non-root driver installation fails with EACCES. Preserve the described isolated reproduction as a repeatable provisioning test: force the initial install to fail, verify the elevated invocation uses a separate cache and subsequent driver installation retains a writable user cache, and include a successful-first-install control that never invokes sudo. No device/UI automation is needed.
There was a problem hiding this comment.
Same answer as the earlier thread (4617808542). This PR changes only CI provisioning MSBuild logic, and our policy for this work is not to add checked-in tests for pipeline or provisioning configuration. These changes are validated by real execution and CI instead.
I validated it by running the real ProvisionAppium target locally in a reproducible harness: shimmed npm whose first global install fails with ERR_SOCKET_TIMEOUT, and a shimmed sudo that makes new files non-writable to emulate root ownership. Results:
- Baseline (fix reverted), red: the elevated install leaves non-writable entries in
~/.npm, and the next driver install fails with EACCES. - Fix, green: the elevated install uses the separate temp cache,
~/.npmhas 0 non-writable entries, and all three drivers install. - Fix reverted again, red. Fix restored, green.
- Control (no network failure), green.
- TMPDIR edge cases: covered in the other thread.
The next check is this PR's own CI, where the Mac "Install Appium" step must stay green.
Auto-replied by the GitHub Copilot app
Note
Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!
Target Branch
mainnet11, for reviewFor .NET 11 fixes, only a maintainer retargets the approved PR to the active
inflight lane and merges it after CI validates that final base. Do not merge
individual fixes directly into
net11; agents must not retarget or merge.Description of Change
CI hardening only. No product, test, or pipeline YAML changes.
Symptom. Candidate UI build 1687815 (#40116), job Controls Lifecycle,ManualReview,Maps on Mac, failed in Install Appium before any test ran. All 5 RetryHelper attempts failed. Log 4334 shows this chain:
npm list -g appium→ not installed.npm i --location=global appium@2.17.1fails with a transientERR_SOCKET_TIMEOUT.InstallAppiumfalls back tosudo -E npm i --location=global appium@2.17.1, which succeeds.appium driver install uiautomator2@4.2.1(runs as the agent user) fails withEACCES: mkdir '/Users/cloudtest/.npm/_cacache/index-v5/…'. npm explains: "Your cache folder contains root-owned files … sudo chown -R 501:20 /Users/cloudtest/.npm".EACCES/EEXIST), then onENOTEMPTYdebris in.appium/node_modules/appium.Root cause.
sudo -EpreservesHOME, so the elevated npm uses and populates the user's~/.npm/_cacacheas root. Every later non-root npm call on the agent then fails. A single network blip becomes a job failure that retries cannot fix.Same signature elsewhere (UI pipeline 313, builds since 11-13):
EACCESon/home/cloudtest/.npm/_cacache.The next job on AcesShared 107 (log 5398) passed, so the damage is per job, not per machine.
Fix. In
src/Provisioning/Provisioning.csproj, the macOS/LinuxElevatedNpmCommandnow passes--cache "<temp>/maui-elevated-npm-cache". npm keeps its logs and update-notifier state under the cache directory too, so the elevated install no longer writes anything into~/.npm. The normal (non-elevated) path, the Windows elevation command, versions, timeouts, and retry budgets are unchanged.Validation. Real root was unavailable locally, so I emulated it:
ui-tests-steps.yml.HOME,APPIUM_HOME, and the npm global prefix were isolated.sudoshim runs the command, then removes write permission from every path that command created. This matches what a non-root user can do with root-owned 755/644 entries.npmshim fails the first non-elevatednpm i --location=global appium@…withERR_SOCKET_TIMEOUT.Each state ran the real target,
dotnet build ./src/Provisioning/Provisioning.csproj -t:ProvisionAppium -p:SkipAppiumDoctor=true, repeated up to CI's 5 attempts:origin/net11)appium driver install uiautomator2@4.2.1fails with npm's "cache folder contains root-owned files"EACCES; 3,612 non-writable entries left in~/.npm.HOMEorAPPIUM_HOME.EACCES.In the emulation, the elevated install created nothing under
APPIUM_HOME, so isolating the npm cache is enough. Other checks:xmllint --noouton the csproj andgit diff --checkboth pass. Real CI on this PR exercises the normal path; the elevated path only runs after a network failure.I found no open or recently merged PR that changes the elevated npm install.
Provisioning.csprojis byte-identical onnet11,main,inflight/current, andcandidate/11.0.1xx-sr2.Issues Fixed
No tracking issue. Found while analysing candidate PR #40116 CI (UI build 1687815).