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

node: Prepare for a larger Npm refactoring #9385

Merged
merged 6 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* License-Filename: LICENSE
*/

package org.ossreviewtoolkit.plugins.packagemanagers.node
package org.ossreviewtoolkit.plugins.packagemanagers.node.npm

import io.kotest.core.spec.style.WordSpec
import io.kotest.engine.spec.tempdir
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* License-Filename: LICENSE
*/

package org.ossreviewtoolkit.plugins.packagemanagers.node
package org.ossreviewtoolkit.plugins.packagemanagers.node.yarn

import io.kotest.core.spec.style.WordSpec
import io.kotest.matchers.should
Expand Down
134 changes: 0 additions & 134 deletions plugins/package-managers/node/src/main/kotlin/Yarn.kt

This file was deleted.

182 changes: 182 additions & 0 deletions plugins/package-managers/node/src/main/kotlin/npm/Npm.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/*
* Copyright (C) 2017 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/

@file:Suppress("TooManyFunctions")

package org.ossreviewtoolkit.plugins.packagemanagers.node.npm

import java.io.File

import org.apache.logging.log4j.kotlin.logger

import org.ossreviewtoolkit.analyzer.AbstractPackageManagerFactory
import org.ossreviewtoolkit.model.config.AnalyzerConfiguration
import org.ossreviewtoolkit.model.config.PackageManagerConfiguration
import org.ossreviewtoolkit.model.config.RepositoryConfiguration
import org.ossreviewtoolkit.plugins.packagemanagers.node.PackageJson
import org.ossreviewtoolkit.plugins.packagemanagers.node.utils.NodePackageManager
import org.ossreviewtoolkit.plugins.packagemanagers.node.utils.NpmDetection
import org.ossreviewtoolkit.plugins.packagemanagers.node.yarn.Yarn
import org.ossreviewtoolkit.utils.common.Os
import org.ossreviewtoolkit.utils.common.ProcessCapture
import org.ossreviewtoolkit.utils.common.withoutPrefix

import org.semver4j.RangesList
import org.semver4j.RangesListFactory

/**
* The [Node package manager](https://www.npmjs.com/) for JavaScript.
*
* This package manager supports the following [options][PackageManagerConfiguration.options]:
* - *legacyPeerDeps*: If true, the "--legacy-peer-deps" flag is passed to NPM to ignore conflicts in peer dependencies
* which are reported since NPM 7. This allows to analyze NPM 6 projects with peer dependency conflicts. For more
* information see the [documentation](https://docs.npmjs.com/cli/v8/commands/npm-install#strict-peer-deps) and the
* [NPM Blog](https://blog.npmjs.org/post/626173315965468672/npm-v7-series-beta-release-and-semver-major).
*/
class Npm(
name: String,
analysisRoot: File,
analyzerConfig: AnalyzerConfiguration,
repoConfig: RepositoryConfiguration
) : Yarn(name, analysisRoot, analyzerConfig, repoConfig) {
companion object {
/** Name of the configuration option to toggle legacy peer dependency support. */
const val OPTION_LEGACY_PEER_DEPS = "legacyPeerDeps"
}

class Factory : AbstractPackageManagerFactory<Npm>("NPM") {
override val globsForDefinitionFiles = listOf("package.json")

override fun create(
analysisRoot: File,
analyzerConfig: AnalyzerConfiguration,
repoConfig: RepositoryConfiguration
) = Npm(type, analysisRoot, analyzerConfig, repoConfig)

Check warning on line 70 in plugins/package-managers/node/src/main/kotlin/npm/Npm.kt

View check run for this annotation

Codecov / codecov/patch

plugins/package-managers/node/src/main/kotlin/npm/Npm.kt#L70

Added line #L70 was not covered by tests
}

private val legacyPeerDeps = options[OPTION_LEGACY_PEER_DEPS].toBoolean()

private val npmViewCache = mutableMapOf<String, PackageJson>()

override fun hasLockfile(projectDir: File) = NodePackageManager.NPM.hasLockfile(projectDir)

override fun command(workingDir: File?) = if (Os.isWindows) "npm.cmd" else "npm"

override fun getVersionRequirement(): RangesList = RangesListFactory.create("6.* - 10.*")

Check warning on line 81 in plugins/package-managers/node/src/main/kotlin/npm/Npm.kt

View check run for this annotation

Codecov / codecov/patch

plugins/package-managers/node/src/main/kotlin/npm/Npm.kt#L81

Added line #L81 was not covered by tests

override fun mapDefinitionFiles(definitionFiles: List<File>) =
NpmDetection(definitionFiles).filterApplicable(NodePackageManager.NPM)

Check warning on line 84 in plugins/package-managers/node/src/main/kotlin/npm/Npm.kt

View check run for this annotation

Codecov / codecov/patch

plugins/package-managers/node/src/main/kotlin/npm/Npm.kt#L84

Added line #L84 was not covered by tests

override fun beforeResolution(definitionFiles: List<File>) {
// We do not actually depend on any features specific to an NPM version, but we still want to stick to a
// fixed minor version to be sure to get consistent results.
checkVersion()
}

Check warning on line 90 in plugins/package-managers/node/src/main/kotlin/npm/Npm.kt

View check run for this annotation

Codecov / codecov/patch

plugins/package-managers/node/src/main/kotlin/npm/Npm.kt#L89-L90

Added lines #L89 - L90 were not covered by tests

override fun getRemotePackageDetails(workingDir: File, packageName: String): PackageJson? {
npmViewCache[packageName]?.let { return it }

return runCatching {
val process = run(workingDir, "info", "--json", packageName)

org.ossreviewtoolkit.plugins.packagemanagers.node.parsePackageJson(process.stdout)
Copy link
Member

Choose a reason for hiding this comment

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

Why is this a FQN now?

Copy link
Member Author

Choose a reason for hiding this comment

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

Just a mistake. Haven't noticed it.

}.onFailure { e ->
logger.warn { "Error getting details for $packageName in directory $workingDir: ${e.message.orEmpty()}" }
}.onSuccess {
npmViewCache[packageName] = it
}.getOrNull()
}

override fun runInstall(workingDir: File): ProcessCapture {
val options = listOfNotNull(
"--ignore-scripts",
"--no-audit",
"--legacy-peer-deps".takeIf { legacyPeerDeps }
)

val subcommand = if (hasLockfile(workingDir)) "ci" else "install"
return ProcessCapture(workingDir, command(workingDir), subcommand, *options.toTypedArray())
}
}

internal fun List<String>.groupLines(vararg markers: 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 ->
markers.firstNotNullOfOrNull { marker ->
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 || line.startsWith("$commonPrefix ")) {
"$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()
}

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

// If no lines but the last end with a dot, assume the message to be a single sentence.
return if (
nonFooterLines.size > 1 &&
nonFooterLines.last().endsWith('.') &&
nonFooterLines.subList(0, nonFooterLines.size - 1).none { it.endsWith('.') }
) {
listOf(nonFooterLines.joinToString(" "))
} else {
nonFooterLines.map { it.trim() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ import org.ossreviewtoolkit.model.config.RepositoryConfiguration
import org.ossreviewtoolkit.model.utils.DependencyGraphBuilder
import org.ossreviewtoolkit.plugins.packagemanagers.node.PackageJson
import org.ossreviewtoolkit.plugins.packagemanagers.node.parsePackageJson
import org.ossreviewtoolkit.plugins.packagemanagers.node.parseProject
import org.ossreviewtoolkit.plugins.packagemanagers.node.utils.NodePackageManager
import org.ossreviewtoolkit.plugins.packagemanagers.node.utils.NpmDetection
import org.ossreviewtoolkit.plugins.packagemanagers.node.utils.parseProject
import org.ossreviewtoolkit.utils.common.CommandLineTool
import org.ossreviewtoolkit.utils.common.Os
import org.ossreviewtoolkit.utils.common.stashDirectories
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import org.ossreviewtoolkit.model.Package
import org.ossreviewtoolkit.model.PackageLinkage
import org.ossreviewtoolkit.model.utils.DependencyHandler
import org.ossreviewtoolkit.plugins.packagemanagers.node.PackageJson
import org.ossreviewtoolkit.plugins.packagemanagers.node.parsePackage
import org.ossreviewtoolkit.plugins.packagemanagers.node.parsePackageJson
import org.ossreviewtoolkit.plugins.packagemanagers.node.pnpm.ModuleInfo.Dependency
import org.ossreviewtoolkit.plugins.packagemanagers.node.utils.parsePackage
import org.ossreviewtoolkit.utils.common.realFile

internal class PnpmDependencyHandler(private val pnpm: Pnpm) : DependencyHandler<Dependency> {
Expand Down
Loading
Loading