Skip to content

Commit

Permalink
fix: remove injected sbt script only if it exists
Browse files Browse the repository at this point in the history
The library was missing a check if the sbt script was injected
successfully. In case it wasn't it failed to remove it because
of a missing cleanup callback.

This patch set refactors the code to handle that case properly.
  • Loading branch information
muscar committed Jul 6, 2021
1 parent 01eaab9 commit 3bbf22b
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import * as types from './types';
import * as tmp from 'tmp';
tmp.setGracefulCleanup();

interface InjectedScript {
path: string;
remove: () => void;
}

const packageFormatVersion = 'mvn:0.0.1';
const sbtCoursierPluginName = 'sbt-coursier';
const sbtDependencyGraphPluginName = 'sbt-dependency-graph';
Expand Down Expand Up @@ -92,9 +97,7 @@ async function legacyInspect(root: string, targetFile: string, options: any) {
}
}

async function getInjectedScriptPath(sbtPluginPath: string, targetFilePath: string):
Promise<{injectedScripPath: string, cleanupCallback?: () => void}> {

async function injectSbtScript(sbtPluginPath: string, targetFilePath: string): Promise<InjectedScript> {
try {
// We could be running from a bundled CLI generated by `pkg`.
// The Node filesystem in that case is not real: https://github.com/zeit/pkg#snapshot-filesystem
Expand All @@ -104,7 +107,7 @@ async function getInjectedScriptPath(sbtPluginPath: string, targetFilePath: stri
dir: path.resolve(targetFilePath, 'project/'),
});
fs.createReadStream(sbtPluginPath).pipe(fs.createWriteStream(tmpSbtPlugin.name));
return { injectedScripPath: tmpSbtPlugin.name, cleanupCallback: tmpSbtPlugin.removeCallback };
return { path: tmpSbtPlugin.name, remove: tmpSbtPlugin.removeCallback };
} catch (error) {
error.message = error.message + '\n\n' +
'Failed to create a temporary file to host Snyk script for SBT build analysis.';
Expand All @@ -128,14 +131,8 @@ function generateSbtPluginPath(sbtVersion: string): string {
}
}

function removeTmpInjectedScript(injectedScripPath) {
if (injectedScripPath.cleanupCallback) {
injectedScripPath.cleanupCallback();
}
}

async function pluginInspect(root: string, targetFile: string, options: any): Promise<types.PluginResult | null> {
let injectedScripPathPromise;
let injectedScript: InjectedScript | undefined;
try {
const targetFilePath = path.dirname(path.resolve(root, targetFile));
const sbtArgs = buildArgs(options.args, false, true);
Expand All @@ -144,8 +141,8 @@ async function pluginInspect(root: string, targetFile: string, options: any): Pr
const packageName = path.basename(root);
const packageVersion = '1.0.0';

injectedScripPathPromise = await getInjectedScriptPath(sbtPluginPath, targetFilePath);
debug('injectedScripPath: ' + injectedScripPathPromise.injectedScripPath);
injectedScript = await injectSbtScript(sbtPluginPath, targetFilePath);
debug('injectedScript.path: ' + injectedScript.path);
const stdout = await subProcess.execute('sbt', sbtArgs, {cwd: targetFilePath});
return {
plugin: {
Expand All @@ -160,7 +157,9 @@ async function pluginInspect(root: string, targetFile: string, options: any): Pr
} finally {
// in case of subProcess.execute failing, perform cleanup here, as putting it after `getInjectScriptPath` might
// not be executed because of `sbt` failing
removeTmpInjectedScript(injectedScripPathPromise);
if (injectedScript) {
injectedScript.remove();
}
}
}

Expand Down

0 comments on commit 3bbf22b

Please sign in to comment.