Skip to content

Commit

Permalink
Ensure directory reliably (also covering concurrent creation). (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
n-g authored Sep 3, 2024
1 parent 402755b commit b3ce473
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
15 changes: 12 additions & 3 deletions .github/workflows/go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,35 @@ jobs:
go:
strategy:
matrix:
go-version: [1.21.x]
go-version: [1.23.x]
runs-on:
- namespace-profile-e2e-small
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Install Go
uses: actions/setup-go@v3
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
cache: false

- name: Check existing directories
run: |
ls -al /home/runner/go
- name: Setup Go cache
uses: ./ # Uses an action in the root directory
with:
cache: go

- name: Run a Go build
run: |
go install namespacelabs.dev/foundation/cmd/ns@latest
- name: Print cache metadata file
run: cat /cache/.ns/cache-metadata.json

- name: Breakpoint on failure
if: failure() && github.ref_name == 'main'
uses: namespacelabs/breakpoint-action@v0
Expand Down
19 changes: 17 additions & 2 deletions dist/index/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27134,6 +27134,7 @@ var io = __nccwpck_require__(7436);




const Env_CacheRoot = "NSC_CACHE_PATH";
const StatePathsKey = "paths";
const privateNamespaceDir = ".ns";
Expand All @@ -27155,9 +27156,23 @@ async function sudoMkdirP(path) {
const userColonGroup = `${uid}:${gid}`;
const anc = ancestors(path);
for (const p of anc) {
if (external_node_fs_namespaceObject.existsSync(p))
if (external_node_fs_namespaceObject.existsSync(p)) {
core.debug(`${p} already exists`);
continue;
await lib_exec.exec("sudo", ["mkdir", p]);
}
const { exitCode, stderr } = await lib_exec.getExecOutput("sudo", ["mkdir", p], {
silent: true,
ignoreReturnCode: true,
});
if (exitCode > 0) {
// Sadly, the exit code is 1 and we cannot match for EEXIST in case of concurrent directory creation.
if (external_node_fs_namespaceObject.existsSync(p)) {
core.debug(`${p} was concurrently created`);
continue;
}
core.info(stderr);
throw new Error(`'sudo mkdir ${p}' failed with exit code ${exitCode}`);
}
await lib_exec.exec("sudo", ["chown", userColonGroup, p]);
}
}
Expand Down
28 changes: 26 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as path from "node:path";
import * as core from "@actions/core";
import * as exec from "@actions/exec";
import * as fs from "node:fs";

Expand Down Expand Up @@ -34,8 +35,31 @@ export async function sudoMkdirP(path: string) {

const anc = ancestors(path);
for (const p of anc) {
if (fs.existsSync(p)) continue;
await exec.exec("sudo", ["mkdir", p]);
if (fs.existsSync(p)) {
core.debug(`${p} already exists`);
continue;
}

const { exitCode, stderr } = await exec.getExecOutput(
"sudo",
["mkdir", p],
{
silent: true,
ignoreReturnCode: true,
}
);

if (exitCode > 0) {
// Sadly, the exit code is 1 and we cannot match for EEXIST in case of concurrent directory creation.
if (fs.existsSync(p)) {
core.debug(`${p} was concurrently created`);
continue;
}

core.info(stderr);
throw new Error(`'sudo mkdir ${p}' failed with exit code ${exitCode}`);
}

await exec.exec("sudo", ["chown", userColonGroup, p]);
}
}
Expand Down

0 comments on commit b3ce473

Please sign in to comment.