Skip to content

Commit

Permalink
fix: catch isogit internal error
Browse files Browse the repository at this point in the history
  • Loading branch information
cristiand391 committed Aug 28, 2023
1 parent df1c97d commit 032103f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 73 deletions.
97 changes: 63 additions & 34 deletions src/shared/localShadowRepo.ts
Expand Up @@ -20,6 +20,19 @@ const getGitDir = (orgId: string, projectPath: string): string =>
// filenames were normalized when read from isogit
const toFilenames = (rows: StatusRow[]): string[] => rows.map((row) => row[FILE]);

// catch isogit's `InternalError` to avoid people report CLI issues in isogit repo.
// See: https://github.com/forcedotcom/cli/issues/2416
const redirectToCliRepoError = (e: Error): never => {
if (e instanceof git.Errors.InternalError) {
const error = new SfError(
`An internal error caused this command to fail. isomorphic-git error:${os.EOL}${e.data.message}`,
e.name
);
throw error;
}
throw e;
};

interface ShadowRepoOptions {
orgId: string;
projectPath: string;
Expand Down Expand Up @@ -93,7 +106,11 @@ export class ShadowRepo {
*/
public async gitInit(): Promise<void> {
await fs.promises.mkdir(this.gitDir, { recursive: true });
await git.init({ fs, dir: this.projectPath, gitdir: this.gitDir, defaultBranch: 'main' });
try {
await git.init({ fs, dir: this.projectPath, gitdir: this.gitDir, defaultBranch: 'main' });
} catch (e) {
redirectToCliRepoError(e as Error);
}
}

/**
Expand Down Expand Up @@ -126,23 +143,27 @@ export class ShadowRepo {
.map((dir) => path.relative(this.projectPath, dir.fullPath))
.map((p) => (this.isWindows ? p.split(path.sep).join(path.posix.sep) : p));

// status hasn't been initialized yet
this.status = await git.statusMatrix({
fs,
dir: this.projectPath,
gitdir: this.gitDir,
filepaths,
ignored: true,
filter: (f) =>
// no hidden files
!f.includes(`${path.sep}.`) &&
// no lwc tests
!isLwcLocalOnlyTest(f) &&
// no gitignore files
!f.endsWith('.gitignore') &&
// isogit uses `startsWith` for filepaths so it's possible to get a false positive
filepaths.some((pkgDir) => pathIsInFolder(f, pkgDir)),
});
try {
// status hasn't been initialized yet
this.status = await git.statusMatrix({
fs,
dir: this.projectPath,
gitdir: this.gitDir,
filepaths,
ignored: true,
filter: (f) =>
// no hidden files
!f.includes(`${path.sep}.`) &&
// no lwc tests
!isLwcLocalOnlyTest(f) &&
// no gitignore files
!f.endsWith('.gitignore') &&
// isogit uses `startsWith` for filepaths so it's possible to get a false positive
filepaths.some((pkgDir) => pathIsInFolder(f, pkgDir)),
});
} catch (e) {
redirectToCliRepoError(e as Error);
}
// isomorphic-git stores things in unix-style tree. Convert to windows-style if necessary
if (this.isWindows) {
this.status = this.status.map((row) => [path.normalize(row[FILE]), row[HEAD], row[WORKDIR], row[3]]);
Expand Down Expand Up @@ -219,7 +240,7 @@ export class ShadowRepo {
deletedFiles = [],
message = 'sfdx source tracking',
needsUpdatedStatus = true,
}: CommitRequest = {}): Promise<string> {
}: CommitRequest = {}): Promise<string | undefined> {
// if no files are specified, commit all changes
if (deployedFiles.length === 0 && deletedFiles.length === 0) {
// this is valid, might not be an error
Expand Down Expand Up @@ -259,28 +280,36 @@ export class ShadowRepo {
error.setData(e.errors);
throw error;
}
throw e;
redirectToCliRepoError(e as Error);
}
}
}

for (const filepath of [...new Set(deletedFiles)]) {
// these need to be done sequentially because isogit manages file locking. Isogit remove does not support multiple files at once
// eslint-disable-next-line no-await-in-loop
await git.remove({ fs, dir: this.projectPath, gitdir: this.gitDir, filepath });
try {
// these need to be done sequentially because isogit manages file locking. Isogit remove does not support multiple files at once
// eslint-disable-next-line no-await-in-loop
await git.remove({ fs, dir: this.projectPath, gitdir: this.gitDir, filepath });
} catch (e) {
redirectToCliRepoError(e as Error);
}
}

const sha = await git.commit({
fs,
dir: this.projectPath,
gitdir: this.gitDir,
message,
author: { name: 'sfdx source tracking' },
});
// status changed as a result of the commit. This prevents users from having to run getStatus(true) to avoid cache
if (needsUpdatedStatus) {
await this.getStatus(true);
try {
const sha = await git.commit({
fs,
dir: this.projectPath,
gitdir: this.gitDir,
message,
author: { name: 'sfdx source tracking' },
});
// status changed as a result of the commit. This prevents users from having to run getStatus(true) to avoid cache
if (needsUpdatedStatus) {
await this.getStatus(true);
}
return sha;
} catch (e) {
redirectToCliRepoError(e as Error);
}
return sha;
}
}
43 changes: 4 additions & 39 deletions yarn.lock
Expand Up @@ -584,30 +584,7 @@
proper-lockfile "^4.1.2"
ts-retry-promise "^0.7.0"

"@salesforce/core@^5.2.0":
version "5.2.0"
resolved "https://registry.yarnpkg.com/@salesforce/core/-/core-5.2.0.tgz#7d18439abe3d33432bae1ec9edc700430ae4ca4a"
integrity sha512-v5jX/7GLyZOrO9smSgtCL6zallVWIb9ryjTzAkJyB7KsHYNEKZKL9TM6uXHFM7SMYjllJQujup54EHxNbL6ezQ==
dependencies:
"@salesforce/kit" "^3.0.6"
"@salesforce/schemas" "^1.6.0"
"@salesforce/ts-types" "^2.0.5"
"@types/semver" "^7.5.0"
ajv "^8.12.0"
change-case "^4.1.2"
faye "^1.4.0"
form-data "^4.0.0"
js2xmlparser "^4.0.1"
jsforce "^2.0.0-beta.27"
jsonwebtoken "9.0.1"
jszip "3.10.1"
pino "^8.14.2"
pino-abstract-transport "^1.0.0"
pino-pretty "^10.2.0"
proper-lockfile "^4.1.2"
ts-retry-promise "^0.7.0"

"@salesforce/core@^5.2.1":
"@salesforce/core@^5.2.0", "@salesforce/core@^5.2.1":
version "5.2.1"
resolved "https://registry.yarnpkg.com/@salesforce/core/-/core-5.2.1.tgz#299bddae7d0705c773b194be8994e5730735e2b4"
integrity sha512-QMx11A0KA/Vl+Ckmz83cw+fiUCMOmsUD8CA3987wAkfJOgxtqyhT4R1R8tj7ad8flQydKUyL3o4UE/2u82tXOw==
Expand Down Expand Up @@ -674,7 +651,7 @@
typescript "^4.9.5"
wireit "^0.9.5"

"@salesforce/kit@^3.0.4", "@salesforce/kit@^3.0.6", "@salesforce/kit@^3.0.9":
"@salesforce/kit@^3.0.4", "@salesforce/kit@^3.0.9":
version "3.0.9"
resolved "https://registry.yarnpkg.com/@salesforce/kit/-/kit-3.0.9.tgz#6674559e27914a9c79dd4283eb94eaea6177fe87"
integrity sha512-L9xeKGdwT4q40JOZteUWgyul+xpRENNIVg336cgXYrFWs+/+7hrWlV4YZdMkLg9WRCZb1HjERJ9gHNiEKXXriA==
Expand Down Expand Up @@ -720,14 +697,7 @@
sinon "^5.1.1"
tslib "^2.5.3"

"@salesforce/ts-types@^2.0.1", "@salesforce/ts-types@^2.0.2", "@salesforce/ts-types@^2.0.5", "@salesforce/ts-types@^2.0.6":
version "2.0.6"
resolved "https://registry.yarnpkg.com/@salesforce/ts-types/-/ts-types-2.0.6.tgz#659590ee6ece45213bbdb23c9e244d206a2f912e"
integrity sha512-hGSU3pwZKItAw567cD2hf+nwe4yPVANonb1E28bLVaRjYAI6FmdxjjTxA+wZRrTpTCpjd5TY67Lq2X1X1lY8bA==
dependencies:
tslib "^2.6.1"

"@salesforce/ts-types@^2.0.7":
"@salesforce/ts-types@^2.0.1", "@salesforce/ts-types@^2.0.2", "@salesforce/ts-types@^2.0.5", "@salesforce/ts-types@^2.0.6", "@salesforce/ts-types@^2.0.7":
version "2.0.7"
resolved "https://registry.yarnpkg.com/@salesforce/ts-types/-/ts-types-2.0.7.tgz#02a6999d0b0e7bcd6c6d8ce621c79fa61af24701"
integrity sha512-8csXgstPuy6QXL3JavkIi/f8DOWHBNCvWeszrFu5sbVlcKO3YqOOCE+rDFGPkrZsYv5OywV6H8kEi877bWOz6Q==
Expand Down Expand Up @@ -5454,12 +5424,7 @@ tslib@^1.8.1, tslib@^1.9.0:
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"
integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==

tslib@^2.0.1, tslib@^2.0.3, tslib@^2.5.3, tslib@^2.6.1:
version "2.6.1"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.6.1.tgz#fd8c9a0ff42590b25703c0acb3de3d3f4ede0410"
integrity sha512-t0hLfiEKfMUoqhG+U1oid7Pva4bbDPHYfJNiB7BiIjRkj1pyC++4N3huJfqY6aRH6VTB0rvtzQwjM4K6qpfOig==

tslib@^2.6.2:
tslib@^2.0.1, tslib@^2.0.3, tslib@^2.5.3, tslib@^2.6.1, tslib@^2.6.2:
version "2.6.2"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.6.2.tgz#703ac29425e7b37cd6fd456e92404d46d1f3e4ae"
integrity sha512-AEYxH93jGFPn/a2iVAwW87VuUIkR1FVUKB77NwMF7nBTDkDrrT/Hpt/IrCJ0QXhW27jTBDcf5ZY7w6RiqTMw2Q==
Expand Down

0 comments on commit 032103f

Please sign in to comment.