{"record":{"id":"706d75834379b10a","repo":"yarnpkg/yarn","slug":"importsourcefilescorrupted","errorCode":null,"errorMessage":"importSourceFilesCorrupted","messagePattern":"importSourceFilesCorrupted","errorType":"exception","errorClass":"MessageError","httpStatus":null,"severity":"error","filePath":"src/cli/commands/import.js","lineNumber":356,"sourceCode":"    activity.end();\n    this.activity = null;\n  }\n}\n\nexport class Import extends Install {\n  constructor(flags: Object, config: Config, reporter: Reporter, lockfile: Lockfile) {\n    super(flags, config, reporter, lockfile);\n    this.resolver = new ImportPackageResolver(this.config, this.lockfile);\n    this.linker = new PackageLinker(config, this.resolver);\n  }\n  createLogicalDependencyTree(packageJson: ?string, packageLock: ?string) {\n    invariant(packageJson, 'package.json should exist');\n    invariant(packageLock, 'package-lock.json should exist');\n    invariant(this.resolver instanceof ImportPackageResolver, 'resolver should be an ImportPackageResolver');\n    try {\n      this.resolver.dependencyTree = new LogicalDependencyTree(packageJson, packageLock);\n    } catch (e) {\n      throw new MessageError(this.reporter.lang('importSourceFilesCorrupted'));\n    }\n  }\n  async getExternalLockfileContents(): Promise<{packageJson: ?string, packageLock: ?string}> {\n    try {\n      const [packageJson, packageLock] = await Promise.all([\n        fs.readFile(path.join(this.config.cwd, NODE_PACKAGE_JSON)),\n        fs.readFile(path.join(this.config.cwd, NPM_LOCK_FILENAME)),\n      ]);\n      return {packageJson, packageLock};\n    } catch (e) {\n      return {packageJson: null, packageLock: null};\n    }\n  }\n  async init(): Promise<Array<string>> {\n    if (await fs.exists(path.join(this.config.cwd, LOCKFILE_FILENAME))) {\n      throw new MessageError(this.reporter.lang('lockfileExists'));\n    }\n    const {packageJson, packageLock} = await this.getExternalLockfileContents();","sourceCodeStart":338,"sourceCodeEnd":374,"githubUrl":"https://github.com/yarnpkg/yarn/blob/c2dda503f3759b5be5f0e24ecd9cf5c97a540147/src/cli/commands/import.js#L338-L374","documentation":"`createLogicalDependencyTree` constructs a `LogicalDependencyTree` from the package.json and package-lock.json strings. Any exception from that constructor is caught and rethrown at `import.js:356` as `importSourceFilesCorrupted` ('Failed to import from package-lock.json, source file(s) corrupted'). The original error is swallowed, so the exact parse/structure failure is hidden behind this generic message.","triggerScenarios":"`new LogicalDependencyTree(packageJson, packageLock)` throws — e.g. either file fails JSON parse, or the lockfile structure (lockfileVersion, dependencies tree) is not what the parser expects.","commonSituations":"npm 7+ `package-lock.json` (lockfileVersion 2 or 3) which yarn v1's importer does not understand; a hand-edited or git-merge-corrupted lockfile; truncated file from a failed write.","solutions":["Regenerate the lockfile with a compatible npm version (`npm install` using npm 5/6 produces lockfileVersion 1).","Resolve git merge-conflict markers in package-lock.json.","If on npm 7+, delete package-lock.json and let yarn import fall back to node_modules."],"exampleFix":"// before (npm 7 lockfileVersion: 3)\n$ yarn import\n→ Failed to import from package-lock.json, source file(s) corrupted\n\n// after\n$ rm package-lock.json\n$ npm install --use-cli-version=npm6   # or: rely on node_modules import\n$ yarn import","handlingStrategy":"try-catch","validationCode":"import semver from 'semver';\n\nfunction lockfileVersionSupported(raw: string): boolean {\n  let lock: any;\n  try { lock = JSON.parse(raw); } catch { return false; }\n  // yarn v1 import supports npm lockfileVersion 1\n  return typeof lock.lockfileVersion === 'number' && lock.lockfileVersion <= 2 === false ? lock.lockfileVersion === 1 : lock.lockfileVersion === 1;\n}","typeGuard":"function isNpmLockfileV1(raw: string): boolean {\n  try {\n    const j = JSON.parse(raw);\n    return j && j.lockfileVersion === 1;\n  } catch { return false; }\n}","tryCatchPattern":"try {\n  await yarnImport();\n} catch (e) {\n  if (/source file\\(s\\) corrupted/.test(e.message)) {\n    await fs.unlink('package-lock.json'); // force node_modules fallback\n    await yarnImport();\n  } else throw e;\n}","preventionTips":["Generate package-lock.json with npm 5/6 (lockfileVersion 1) for yarn v1 import.","Never hand-edit or partially merge package-lock.json.","Run `npm ci` to validate the lockfile before migration."],"tags":["yarn-import","package-lock","corruption","parsing"],"backgroundTag":null,"analyzedSha":"c2dda503f3759b5be5f0e24ecd9cf5c97a540147","analyzedAt":"2026-08-13T04:17:06.305Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}