Skip to content

Commit 57a667f

Browse files
authored
fix: set cwd for mvn invocations to manifest dir (#213)
1 parent ec552dc commit 57a667f

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

src/providers/base_java.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ export default class Base_Java {
127127
* Exists for stubbing in tests.
128128
* @param bin - the command to be invoked
129129
* @param args - the args to pass to the binary
130+
* @param {import('child_process').ExecFileOptionsWithStringEncoding} [opts={}]
130131
* @protected
131132
*/
132133
_invokeCommand(bin, args, opts={}) { return invokeCommand(bin, args, opts) }
@@ -138,14 +139,15 @@ export default class Base_Java {
138139
* @returns string
139140
*/
140141
selectToolBinary(manifestPath, opts) {
142+
const manifestDir = path.dirname(manifestPath)
141143
const toolPath = getCustomPath(this.globalBinary, opts)
142144

143145
const useWrapper = getWrapperPreference(toolPath, opts)
144146
if (useWrapper) {
145147
const wrapper = this.traverseForWrapper(manifestPath)
146148
if (wrapper !== undefined) {
147149
try {
148-
this._invokeCommand(wrapper, ['--version'])
150+
this._invokeCommand(wrapper, ['--version'], {cwd: manifestDir})
149151
} catch (error) {
150152
throw new Error(`failed to check for ${this.localWrapper}`, {cause: error})
151153
}
@@ -154,7 +156,7 @@ export default class Base_Java {
154156
}
155157
// verify tool is accessible, if wrapper was not requested or not found
156158
try {
157-
this._invokeCommand(toolPath, ['--version'])
159+
this._invokeCommand(toolPath, ['--version'], {cwd: manifestDir})
158160
} catch (error) {
159161
if (error.code === 'ENOENT') {
160162
throw new Error((useWrapper ? `${this.localWrapper} not found and ` : '') + `${this.globalBinary === 'mvn' ? 'maven' : 'gradle'} not found at ${toolPath}`)

src/providers/java_maven.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,12 @@ export default class Java_maven extends Base_java {
7676
* @private
7777
*/
7878
#createSbomStackAnalysis(manifest, opts = {}) {
79+
const manifestDir = path.dirname(manifest)
7980
const mvn = this.selectToolBinary(manifest, opts)
8081

8182
// clean maven target
8283
try {
83-
this._invokeCommand(mvn, ['-q', 'clean', '-f', manifest])
84+
this._invokeCommand(mvn, ['-q', 'clean'], {cwd: manifestDir})
8485
} catch (error) {
8586
throw new Error(`failed to clean maven target`, {cause: error})
8687
}
@@ -89,7 +90,7 @@ export default class Java_maven extends Base_java {
8990
let tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'exhort_'))
9091
let tmpDepTree = path.join(tmpDir, 'mvn_deptree.txt')
9192
// build initial command (dot outputType is not available for verbose mode)
92-
let depTreeCmdArgs = ['-q', 'org.apache.maven.plugins:maven-dependency-plugin:3.6.0:tree', '-Dverbose', '-DoutputType=text', `-DoutputFile=${tmpDepTree}`, '-f', manifest]
93+
let depTreeCmdArgs = ['-q', 'org.apache.maven.plugins:maven-dependency-plugin:3.6.0:tree', '-Dverbose', '-DoutputType=text', `-DoutputFile=${tmpDepTree}`]
9394
// exclude ignored dependencies, exclude format is groupId:artifactId:scope:version.
9495
// version and scope are marked as '*' if not specified (we do not use scope yet)
9596
let ignoredDeps = new Array()
@@ -101,7 +102,7 @@ export default class Java_maven extends Base_java {
101102
})
102103
// execute dependency tree command
103104
try {
104-
this._invokeCommand(mvn, depTreeCmdArgs)
105+
this._invokeCommand(mvn, depTreeCmdArgs, {cwd: manifestDir})
105106
} catch (error) {
106107
throw new Error(`failed creating maven dependency tree`, {cause: error})
107108
}
@@ -144,22 +145,21 @@ export default class Java_maven extends Base_java {
144145
const mvn = this.selectToolBinary(manifestPath, opts)
145146

146147
const tmpEffectivePom = path.resolve(path.join(path.dirname(manifestPath), 'effective-pom.xml'))
147-
const targetPom = manifestPath
148148

149149
// create effective pom and save to temp file
150150
try {
151-
this._invokeCommand(mvn, ['-q', 'help:effective-pom', `-Doutput=${tmpEffectivePom}`, '-f', targetPom])
151+
this._invokeCommand(mvn, ['-q', 'help:effective-pom', `-Doutput=${tmpEffectivePom}`], {cwd: path.dirname(manifestPath)})
152152
} catch (error) {
153153
throw new Error(`failed creating maven effective pom`, {cause: error})
154154
}
155155
// iterate over all dependencies in original pom and collect all ignored ones
156-
let ignored = this.#getDependencies(targetPom).filter(d => d.ignore)
156+
let ignored = this.#getDependencies(manifestPath).filter(d => d.ignore)
157157
// iterate over all dependencies and create a package for every non-ignored one
158158
/** @type [Dependency] */
159159
let dependencies = this.#getDependencies(tmpEffectivePom)
160160
.filter(d => !(this.#dependencyIn(d, ignored)) && !(this.#dependencyInExcludingVersion(d, ignored)))
161161
let sbom = new Sbom();
162-
let rootDependency = this.#getRootFromPom(tmpEffectivePom, targetPom);
162+
let rootDependency = this.#getRootFromPom(tmpEffectivePom, manifestPath);
163163
let purlRoot = this.toPurl(rootDependency.groupId, rootDependency.artifactId, rootDependency.version)
164164
sbom.addRoot(purlRoot)
165165
dependencies.forEach(dep => {

0 commit comments

Comments
 (0)