Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty lockfiles will trigger lockfile to init/populate #637

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions src/spec-configuration/containerFeaturesConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ export async function generateFeaturesConfig(params: ContainerFeatureInternalPar

const ociCacheDir = await prepareOCICache(dstFolder);

const lockfile = await readLockfile(config);
const { lockfile, initLockfile } = await readLockfile(config);

const processFeature = async (_userFeature: DevContainerFeature) => {
return await processFeatureIdentifier(params, configPath, workspaceRoot, _userFeature, lockfile);
Expand All @@ -570,7 +570,7 @@ export async function generateFeaturesConfig(params: ContainerFeatureInternalPar
await fetchFeatures(params, featuresConfig, locallyCachedFeatureSet, dstFolder, localFeaturesFolder, ociCacheDir, lockfile);

await logFeatureAdvisories(params, featuresConfig);
await writeLockfile(params, config, featuresConfig);
await writeLockfile(params, config, featuresConfig, initLockfile);
return featuresConfig;
}

Expand All @@ -582,7 +582,7 @@ export async function loadVersionInfo(params: ContainerFeatureInternalParams, co
return { features: {} };
}

const lockfile = await readLockfile(config);
const { lockfile } = await readLockfile(config);

const features: Record<string, any> = {};

Expand Down
14 changes: 9 additions & 5 deletions src/spec-configuration/lockfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface Lockfile {
features: Record<string, { version: string; resolved: string; integrity: string }>;
}

export async function writeLockfile(params: ContainerFeatureInternalParams, config: DevContainerConfig, featuresConfig: FeaturesConfig) {
export async function writeLockfile(params: ContainerFeatureInternalParams, config: DevContainerConfig, featuresConfig: FeaturesConfig, forceInitLockfile?: boolean) {
const lockfilePath = getLockfilePath(config);
const oldLockfileContent = await readLocalFile(lockfilePath)
.catch(err => {
Expand All @@ -22,7 +22,7 @@ export async function writeLockfile(params: ContainerFeatureInternalParams, conf
}
});

if (!oldLockfileContent && !params.experimentalLockfile && !params.experimentalFrozenLockfile) {
if (!forceInitLockfile && !oldLockfileContent && !params.experimentalLockfile && !params.experimentalFrozenLockfile) {
return;
}

Expand Down Expand Up @@ -63,13 +63,17 @@ export async function writeLockfile(params: ContainerFeatureInternalParams, conf
}
}

export async function readLockfile(config: DevContainerConfig): Promise<Lockfile | undefined> {
export async function readLockfile(config: DevContainerConfig): Promise<{ lockfile?: Lockfile; initLockfile?: boolean }> {
try {
const content = await readLocalFile(getLockfilePath(config));
return JSON.parse(content.toString()) as Lockfile;
// If empty file, use as maker to initialize lockfile when build completes.
if (content.toString().trim() === '') {
return { initLockfile: true };
}
return { lockfile: JSON.parse(content.toString()) as Lockfile };
} catch (err) {
if (err?.code === 'ENOENT') {
return undefined;
return {};
}
throw err;
}
Expand Down
2 changes: 1 addition & 1 deletion src/spec-node/featuresCLI/resolveDependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ async function featuresResolveDependencies({
env: process.env,
};

const lockfile = await readLockfile(config);
const { lockfile } = await readLockfile(config);
const processFeature = async (_userFeature: DevContainerFeature) => {
return await processFeatureIdentifier(params, configPath, workspaceFolder, _userFeature, lockfile);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"image": "mcr.microsoft.com/devcontainers/base",
"features": {
"ghcr.io/devcontainers/features/dotnet:2": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"image": "mcr.microsoft.com/devcontainers/base",
"features": {
"ghcr.io/devcontainers/features/dotnet:2": {}
}
}
37 changes: 37 additions & 0 deletions src/test/container-features/lockfile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,41 @@ describe('Lockfile', function () {
assert.equal(response.outcome, 'error');
}
});

it('empty lockfile should init', async () => {
const workspaceFolder = path.join(__dirname, 'configs/lockfile-generate-from-empty-file');
const lockfilePath = path.join(workspaceFolder, '.devcontainer', 'devcontainer-lock.json');
const cleanup = async () => {
await rmLocal(lockfilePath, { force: true });
await shellExec(`touch ${lockfilePath}`);
};

await cleanup();
const res = await shellExec(`${cli} build --workspace-folder ${workspaceFolder}`);
const response = JSON.parse(res.stdout);
assert.equal(response.outcome, 'success');
const actual = JSON.parse((await readLocalFile(lockfilePath)).toString());
assert.ok(actual.features['ghcr.io/devcontainers/features/dotnet:2']);
await cleanup();
});

it('empty lockfile should not init when frozen', async () => {
const workspaceFolder = path.join(__dirname, 'configs/lockfile-generate-from-empty-file-frozen');
const lockfilePath = path.join(workspaceFolder, '.devcontainer', 'devcontainer-lock.json');
const cleanup = async () => {
await rmLocal(lockfilePath, { force: true });
await shellExec(`touch ${lockfilePath}`);
};

await cleanup();
try {
await shellExec(`${cli} build --workspace-folder ${workspaceFolder} --experimental-frozen-lockfile`);
await cleanup();
} catch (res) {
const response = JSON.parse(res.stdout);
assert.equal(response.outcome, 'error');
assert.equal(response.message, 'Lockfile does not match.');
await cleanup();
}
});
});
Loading