Skip to content

Fix: Lockfile should be updated when merge conflcits are resolved #4195

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

Merged
merged 3 commits into from
Aug 18, 2017
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
2 changes: 1 addition & 1 deletion __tests__/commands/_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export async function createLockfile(dir: string): Promise<Lockfile> {
lockfile = parse(rawLockfile).object;
}

return new Lockfile(lockfile);
return new Lockfile({cache: lockfile, parseResultType: 'success'});
}

export function explodeLockfile(lockfile: string): Array<string> {
Expand Down
20 changes: 13 additions & 7 deletions __tests__/lockfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,20 @@ test('Lockfile.fromDirectory', () => {});

test('Lockfile.getLocked', () => {
const lockfile = new Lockfile({
foo: 'bar',
bar: {},
cache: {
foo: 'bar',
bar: {},
},
});
expect(!!lockfile.getLocked('foo')).toBeTruthy();
});

test('Lockfile.getLocked pointer', () => {
const lockfile = new Lockfile({
foo: 'bar',
bar: {},
cache: {
foo: 'bar',
bar: {},
},
});
expect(!!lockfile.getLocked('foo')).toBeTruthy();
});
Expand All @@ -66,8 +70,10 @@ test('Lockfile.getLocked no cache', () => {

test('Lockfile.getLocked defaults', () => {
const pattern = new Lockfile({
foobar: {
version: '0.0.0',
cache: {
foobar: {
version: '0.0.0',
},
},
}).getLocked('foobar');
expect(pattern.registry).toBe('npm');
Expand All @@ -76,7 +82,7 @@ test('Lockfile.getLocked defaults', () => {
});

test('Lockfile.getLocked unknown', () => {
new Lockfile({}).getLocked('foobar');
new Lockfile({cache: {}}).getLocked('foobar');
});

test('Lockfile.getLockfile', () => {
Expand Down
2 changes: 1 addition & 1 deletion __tests__/package-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Config from '../src/config.js';

async function prepareRequest(pattern, version, resolved): Object {
const privateDepCache = {[pattern]: {version, resolved}};
const lockfile = new Lockfile(privateDepCache);
const lockfile = new Lockfile({cache: privateDepCache});
const reporter = new reporters.NoopReporter({});
const depRequestPattern = {
pattern,
Expand Down
2 changes: 1 addition & 1 deletion src/cli/commands/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,6 @@ export function hasWrapper(commander: Object, args: Array<string>): boolean {
}

export async function run(config: Config, reporter: Reporter, flags: Object, args: Array<string>): Promise<void> {
const imp = new Import(flags, config, reporter, new Lockfile({}));
const imp = new Import(flags, config, reporter, new Lockfile({cache: {}}));
await imp.init();
}
11 changes: 7 additions & 4 deletions src/cli/commands/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,14 +347,15 @@ export class Install {
if (!lockfileCache) {
return false;
}
const lockfileClean = this.lockfile.parseResultType === 'success';
const match = await this.integrityChecker.check(patterns, lockfileCache, this.flags, workspaceLayout);
if (this.flags.frozenLockfile && match.missingPatterns.length > 0) {
if (this.flags.frozenLockfile && (!lockfileClean || match.missingPatterns.length > 0)) {
throw new MessageError(this.reporter.lang('frozenLockfileError'));
}

const haveLockfile = await fs.exists(path.join(this.config.lockfileFolder, constants.LOCKFILE_FILENAME));

if (match.integrityMatches && haveLockfile) {
if (match.integrityMatches && haveLockfile && lockfileClean) {
this.reporter.success(this.reporter.lang('upToDate'));
return true;
}
Expand Down Expand Up @@ -708,13 +709,15 @@ export class Install {
const manifest = this.lockfile.getLocked(pattern);
return manifest && manifest.resolved === lockfileBasedOnResolver[pattern].resolved;
});

// remove command is followed by install with force, lockfile will be rewritten in any case then
if (
!this.flags.force &&
this.lockfile.parseResultType === 'success' &&
lockFileHasAllPatterns &&
lockfilePatternsMatch &&
resolverPatternsAreSameAsInLockfile &&
patterns.length &&
!this.flags.force
patterns.length
) {
return;
}
Expand Down
8 changes: 5 additions & 3 deletions src/lockfile/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ type Token = {
value: boolean | number | string | void,
};

type ParseResult = {
type: 'merge' | 'none' | 'conflict',
export type ParseResultType = 'merge' | 'success' | 'conflict';

export type ParseResult = {
type: ParseResultType,
object: Object,
};

Expand Down Expand Up @@ -401,5 +403,5 @@ function parseWithConflict(str: string, fileLoc: string): ParseResult {

export default function(str: string, fileLoc: string = 'lockfile'): ParseResult {
str = stripBOM(str);
return hasMergeConflicts(str) ? parseWithConflict(str, fileLoc) : {type: 'none', object: parse(str, fileLoc)};
return hasMergeConflicts(str) ? parseWithConflict(str, fileLoc) : {type: 'success', object: parse(str, fileLoc)};
}
19 changes: 13 additions & 6 deletions src/lockfile/wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import type {Reporter} from '../reporters/index.js';
import type {Manifest, PackageRemote} from '../types.js';
import type {RegistryNames} from '../registries/index.js';
import type {ParseResultType} from './parse.js';
import {sortAlpha} from '../util/misc.js';
import PackageRequest from '../package-request.js';
import parse from './parse.js';
Expand Down Expand Up @@ -82,9 +83,12 @@ export function explodeEntry(pattern: string, obj: Object): LockManifest {
}

export default class Lockfile {
constructor(cache?: ?Object, source?: string) {
constructor(
{cache, source, parseResultType}: {cache?: ?Object, source?: string, parseResultType?: ParseResultType} = {},
) {
this.source = source || '';
this.cache = cache;
this.parseResultType = parseResultType;
}

// source string if the `cache` was parsed
Expand All @@ -94,33 +98,36 @@ export default class Lockfile {
[key: string]: LockManifest,
};

parseResultType: ?ParseResultType;

static async fromDirectory(dir: string, reporter?: Reporter): Promise<Lockfile> {
// read the manifest in this directory
const lockfileLoc = path.join(dir, constants.LOCKFILE_FILENAME);

let lockfile;
let rawLockfile = '';
let parseResult;

if (await fs.exists(lockfileLoc)) {
rawLockfile = await fs.readFile(lockfileLoc);
const lockResult = parse(rawLockfile, lockfileLoc);
parseResult = parse(rawLockfile, lockfileLoc);

if (reporter) {
if (lockResult.type === 'merge') {
if (parseResult.type === 'merge') {
reporter.info(reporter.lang('lockfileMerged'));
} else if (lockResult.type === 'conflict') {
} else if (parseResult.type === 'conflict') {
reporter.warn(reporter.lang('lockfileConflict'));
}
}

lockfile = lockResult.object;
lockfile = parseResult.object;
} else {
if (reporter) {
reporter.info(reporter.lang('noLockfileFound'));
}
}

return new Lockfile(lockfile, rawLockfile);
return new Lockfile({cache: lockfile, source: rawLockfile, parseResultType: parseResult && parseResult.type});
}

getLocked(pattern: string): ?LockManifest {
Expand Down