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

NPM issue mapping improvements #8177

Merged
merged 3 commits into from
Jan 25, 2024
Merged
Changes from all commits
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
193 changes: 96 additions & 97 deletions plugins/package-managers/node/src/main/kotlin/Npm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -261,33 +261,6 @@ open class Npm(
}
}

private fun isValidNodeModulesDirectory(rootModulesDir: File, modulesDir: File?): Boolean {
if (modulesDir == null) return false

var currentDir: File = modulesDir
while (currentDir != rootModulesDir) {
if (currentDir.name != "node_modules") {
return false
}

currentDir = currentDir.parentFile.parentFile
if (currentDir.name.startsWith("@")) {
currentDir = currentDir.parentFile
}
}

return true
}

private fun nodeModulesDirForPackageJson(packageJson: File): File? {
var modulesDir = packageJson.parentFile.parentFile
if (modulesDir.name.startsWith("@")) {
modulesDir = modulesDir.parentFile
}

return modulesDir.takeIf { it.name == "node_modules" }
}

/**
* Construct a [Package] by parsing its _package.json_ file and - if applicable - querying additional
* content via the `npm view` command. The result is a [Pair] with the raw identifier and the new package.
Expand Down Expand Up @@ -549,17 +522,6 @@ open class Npm(
)
}

private fun findDependencyModuleDir(dependencyName: String, searchModuleDirs: List<File>): List<File> {
searchModuleDirs.forEachIndexed { index, moduleDir ->
// Note: resolve() also works for scoped dependencies, e.g. dependencyName = "@x/y"
val dependencyModuleDir = moduleDir.resolve("node_modules/$dependencyName")
if (dependencyModuleDir.isDirectory) {
return listOf(dependencyModuleDir) + searchModuleDirs.subList(index, searchModuleDirs.size)
}
}
return emptyList()
}

private fun parseProject(packageJson: File): Project {
logger.debug { "Parsing project info from '$packageJson'." }

Expand Down Expand Up @@ -610,67 +572,13 @@ open class Npm(
val lines = process.stderr.lines()
val issues = mutableListOf<Issue>()

fun mapLinesToIssues(prefix: String, severity: Severity) {
val ignorablePrefixes = setOf("code ", "errno ", "path ", "syscall ")
val singleLinePrefixes = setOf("deprecated ", "skipping integrity check for git dependency ")
val minSecondaryPrefixLength = 5

val issueLines = lines.mapNotNull { line ->
line.withoutPrefix(prefix)?.takeUnless { ignorablePrefixes.any { prefix -> it.startsWith(prefix) } }
}

var commonPrefix: String
var previousPrefix = ""

val collapsedLines = issueLines.distinct().fold(mutableListOf<String>()) { messages, line ->
if (messages.isEmpty()) {
// The first line is always added including the prefix. The prefix will be removed later.
messages += line
} else {
// Find the longest common prefix that ends with space.
commonPrefix = line.commonPrefixWith(messages.last())
if (!commonPrefix.endsWith(' ')) {
// Deal with prefixes being used on their own as separators.
commonPrefix = if ("$commonPrefix " == previousPrefix) {
"$commonPrefix "
} else {
commonPrefix.dropLastWhile { it != ' ' }
}
}

if (commonPrefix !in singleLinePrefixes && commonPrefix.length >= minSecondaryPrefixLength) {
// Do not drop the whole prefix but keep the space when concatenating lines.
messages[messages.size - 1] += line.drop(commonPrefix.length - 1).trimEnd()
previousPrefix = commonPrefix
} else {
// Remove the prefix from previously added message start.
messages[messages.size - 1] = messages.last().removePrefix(previousPrefix).trimStart()
messages += line
}
}

messages
}

if (collapsedLines.isNotEmpty()) {
// Remove the prefix from the last added message start.
collapsedLines[collapsedLines.size - 1] = collapsedLines.last().removePrefix(previousPrefix).trimStart()
}

collapsedLines.forEach { line ->
// Skip any footer as a whole.
if (line == "A complete log of this run can be found in:") return

issues += Issue(
source = managerName,
message = line,
severity = severity
)
}
lines.groupLines("npm WARN ").mapTo(issues) {
Issue(source = managerName, message = it, severity = Severity.WARNING)
}

mapLinesToIssues("npm WARN ", Severity.WARNING)
mapLinesToIssues("npm ERR! ", Severity.ERROR)
lines.groupLines("npm ERR! ").mapTo(issues) {
Issue(source = managerName, message = it, severity = Severity.ERROR)
}

return issues
}
Expand All @@ -686,3 +594,94 @@ open class Npm(
return ProcessCapture(workingDir, command(workingDir), subcommand, *options.toTypedArray())
}
}

private fun findDependencyModuleDir(dependencyName: String, searchModuleDirs: List<File>): List<File> {
searchModuleDirs.forEachIndexed { index, moduleDir ->
// Note: resolve() also works for scoped dependencies, e.g. dependencyName = "@x/y"
val dependencyModuleDir = moduleDir.resolve("node_modules/$dependencyName")
if (dependencyModuleDir.isDirectory) {
return listOf(dependencyModuleDir) + searchModuleDirs.subList(index, searchModuleDirs.size)
}
}
return emptyList()
}

private fun isValidNodeModulesDirectory(rootModulesDir: File, modulesDir: File?): Boolean {
if (modulesDir == null) return false

var currentDir: File = modulesDir
while (currentDir != rootModulesDir) {
if (currentDir.name != "node_modules") {
return false
}

currentDir = currentDir.parentFile.parentFile
if (currentDir.name.startsWith("@")) {
currentDir = currentDir.parentFile
}
}

return true
}

private fun nodeModulesDirForPackageJson(packageJson: File): File? {
var modulesDir = packageJson.parentFile.parentFile
if (modulesDir.name.startsWith("@")) {
modulesDir = modulesDir.parentFile
}

return modulesDir.takeIf { it.name == "node_modules" }
}

private fun List<String>.groupLines(marker: String): List<String> {
val ignorableLinePrefixes = setOf("code ", "errno ", "path ", "syscall ")
val singleLinePrefixes = setOf("deprecated ", "skipping integrity check for git dependency ")
val minCommonPrefixLength = 5

val issueLines = mapNotNull { line ->
line.withoutPrefix(marker)?.takeUnless { ignorableLinePrefixes.any { prefix -> it.startsWith(prefix) } }
}

var commonPrefix: String
var previousPrefix = ""

val collapsedLines = issueLines.distinct().fold(mutableListOf<String>()) { messages, line ->
if (messages.isEmpty()) {
// The first line is always added including the prefix. The prefix will be removed later.
messages += line
} else {
// Find the longest common prefix that ends with space.
commonPrefix = line.commonPrefixWith(messages.last())
if (!commonPrefix.endsWith(' ')) {
// Deal with prefixes being used on their own as separators.
commonPrefix = if ("$commonPrefix " == previousPrefix) {
"$commonPrefix "
} else {
commonPrefix.dropLastWhile { it != ' ' }
}
}

if (commonPrefix !in singleLinePrefixes && commonPrefix.length >= minCommonPrefixLength) {
// Do not drop the whole prefix but keep the space when concatenating lines.
messages[messages.size - 1] += line.drop(commonPrefix.length - 1).trimEnd()
previousPrefix = commonPrefix
} else {
// Remove the prefix from previously added message start.
messages[messages.size - 1] = messages.last().removePrefix(previousPrefix).trimStart()
messages += line
}
}

messages
}

if (collapsedLines.isNotEmpty()) {
// Remove the prefix from the last added message start.
collapsedLines[collapsedLines.size - 1] = collapsedLines.last().removePrefix(previousPrefix).trimStart()
}

return collapsedLines.takeWhile {
// Skip any footer as a whole.
it != "A complete log of this run can be found in:"
}
}
Loading