Skip to content
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

[BUG FIX] Fix path resolution for executables #328

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 30 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ plugins {
id("com.cinnober.gradle.semver-git") version "3.0.0"
id("org.jetbrains.dokka") version "1.7.10"
id("org.gradle.test-retry") version "1.5.0"
`maven-publish`
}

group = "com.github.node-gradle"
Expand Down Expand Up @@ -59,6 +60,35 @@ dependencies {
testImplementation("org.mock-server:mockserver-netty:5.15.0")
}

publishing {
publications {
create<MavenPublication>("mavenJava") {
from(components["java"])
groupId = "com.github.node-gradle"
artifactId = "gradle-node-plugin"
version = project.version.toString()
pom {
name.set("Gradle Node.js Plugin")
description.set("Gradle plugin for executing Node.js scripts. Supports npm, pnpm, Yarn, and Bun.")
url.set("https://github.com/node-gradle/gradle-node-plugin")
licenses {
license {
name.set("Apache License, Version 2.0")
url.set("http://www.apache.org/licenses/LICENSE-2.0.txt")
}
}
}
}
}
repositories {
maven {
name = "localMaven"
url = uri("${buildDir}/maven-repo")
}
}
}


tasks.compileTestGroovy {
// Should be
// classpath += files(sourceSets.test.get().kotlin.classesDirectory)
Expand Down
21 changes: 20 additions & 1 deletion src/main/kotlin/com/github/gradle/node/exec/ExecRunner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ fun computeWorkingDir(nodeProjectDir: DirectoryProperty, execConfiguration: Exec
return workingDir
}

/**
* Helper function to find the best matching executable in the system PATH.
*
* @param executableName The name of the executable to search for.
* @return The best matching executable path as a String.
*/
fun findBestExecutableMatch(executableName: String): String {
val pathVariable = System.getenv("PATH") ?: return executableName
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iirc this won't work on windows where environment variables are case-insensitive (and default to Path)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Easy to fix, thanks.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

val paths = pathVariable.split(File.pathSeparator)
for (path in paths) {
val executableFile = File(path, executableName)
if (executableFile.exists() && executableFile.canExecute()) {
return executableFile.absolutePath
}
}
return executableName // Return the original executable if no match is found
}

/**
* Basic execution runner that runs a given ExecConfiguration.
*
Expand All @@ -43,8 +61,9 @@ fun computeWorkingDir(nodeProjectDir: DirectoryProperty, execConfiguration: Exec
*/
class ExecRunner {
fun execute(projectHelper: ProjectApiHelper, extension: NodeExtension, execConfiguration: ExecConfiguration): ExecResult {
val executablePath = findBestExecutableMatch(execConfiguration.executable)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From a quick glance it looks like this will break the download functionality entirely by only using already installed applications

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will address these points. Thanks.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done with tests to validate behaviour (w/ download and without download)

return projectHelper.exec {
executable = execConfiguration.executable
executable = executablePath
args = execConfiguration.args
environment = computeEnvironment(execConfiguration)
isIgnoreExitValue = execConfiguration.ignoreExitValue
Expand Down