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

fix(composer): Restore any modified files after analysis #9590

Merged
merged 1 commit into from
Dec 12, 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
35 changes: 5 additions & 30 deletions plugins/package-managers/composer/src/main/kotlin/Composer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,8 @@ import org.ossreviewtoolkit.utils.ort.showStackTrace

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

private const val COMPOSER_PHAR_BINARY = "composer.phar"
private const val COMPOSER_LOCK_FILE = "composer.lock"
private const val SCOPE_NAME_REQUIRE = "require"
private const val SCOPE_NAME_REQUIRE_DEV = "require-dev"
private val ALL_SCOPE_NAMES = setOf(SCOPE_NAME_REQUIRE, SCOPE_NAME_REQUIRE_DEV)
Expand Down Expand Up @@ -139,7 +137,11 @@ class Composer(
}

val lockfile = stashDirectories(workingDir.resolve("vendor")).use { _ ->
ensureLockfile(workingDir).let {
val lockfileProvider = LockfileProvider(definitionFile)
Copy link
Member

Choose a reason for hiding this comment

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

I might be wrong, but to my understanding ORT's analyzers normally do not do this kind of clean-up.
If true, what's the reason for doing this here?

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree that this kind of cleanup is probably done inconsistently / not done at all for package managers currently. But I believe we should come up with a policy to start doing it consistently. Besides the annoyance of modified files when analyzing locally, leaving modified files behind also creates problems in CI pipelines that use git describe-based mechanisms to calculate the project version after analysis, as such approaches would eventually add a -dirty suffix to the version number.

Copy link
Member

Choose a reason for hiding this comment

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

as such approaches would eventually add a -dirty suffix to the version number.

These could be also easily addressed with dropping file changes via git.

I'm undecided if it's worth to maintain the logic for it, what do others think? maybe any @oss-review-toolkit/core-devs ?

Copy link
Member Author

Choose a reason for hiding this comment

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

These could be also easily addressed with dropping file changes via git.

Our premise has always been that the ORT analyzer can be used without changes to your build, and that includes making fixups like resetting modified files with Git in a CI pipeline in my view.

Note that approving this PR does not mean that you agree to do the same changes for other package managers. As such I'd appreciate if the discussion could be limited to the code in this PR, and to pursue the general discussion elsewhere.

Copy link
Member

@mnonnenmacher mnonnenmacher Dec 12, 2024

Choose a reason for hiding this comment

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

In my opinion we should always try to not modify the project files with the analyzer, and we have to, clean up afterward, and I think that was always our philosophy, although we might have forgotten to implement it consistently. For example, in NPM we could simply delete the node_modules directory, but instead we move it away so that it can be restored after the analysis.


requireLockfile(workingDir) { lockfileProvider.lockfile.isFile }

lockfileProvider.ensureLockfile {
logger.info { "Parsing lockfile at '$it'..." }
parseLockfile(it.readText())
}
Expand Down Expand Up @@ -242,33 +244,6 @@ class Composer(
scopeDependencies = scopes
)
}

private fun ensureLockfile(workingDir: File): File {
val lockfile = workingDir.resolve(COMPOSER_LOCK_FILE)

val hasLockfile = lockfile.isFile
requireLockfile(workingDir) { hasLockfile }
if (hasLockfile) return lockfile

// Ensure that the build is not configured to disallow the creation of lockfiles.
ComposerCommand.run(workingDir, "--no-interaction", "config", "--unset", "lock").requireSuccess()

val composerVersion = Semver(ComposerCommand.getVersion(workingDir))
val args = buildList {
add("--no-interaction")
add("update")
add("--ignore-platform-reqs")

if (composerVersion.major >= 2) {
add("--no-install")
add("--no-audit")
}
}

ComposerCommand.run(workingDir, *args.toTypedArray()).requireSuccess()

return lockfile
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright (C) 2024 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
*/

package org.ossreviewtoolkit.plugins.packagemanagers.composer

import java.io.File

import kotlin.io.path.moveTo

import org.semver4j.Semver

private const val COMPOSER_LOCK_FILE = "composer.lock"

class LockfileProvider(private val definitionFile: File) {
private val workingDir = definitionFile.parentFile

val lockfile = workingDir.resolve(COMPOSER_LOCK_FILE)

fun <T> ensureLockfile(block: (File) -> T): T {
if (lockfile.isFile) return block(lockfile)

val definitionFileBackup = enableLockfileCreation()

return try {
require(createLockFile())
block(lockfile)
} finally {
lockfile.delete()
definitionFileBackup?.also { it.toPath().moveTo(definitionFile.toPath(), overwrite = true) }
}
}

private fun enableLockfileCreation(): File? {
var definitionFileBackup: File? = null
val lockConfig = ComposerCommand.run(workingDir, "--no-interaction", "config", "lock")

if (lockConfig.isSuccess && lockConfig.stdout.trim() == "false") {
File.createTempFile("composer", "json", workingDir).also {
mnonnenmacher marked this conversation as resolved.
Show resolved Hide resolved
// The above call already creates an empty file, so the copy call needs to overwrite it.
definitionFileBackup = definitionFile.copyTo(it, overwrite = true)
}

// Ensure that the build is not configured to disallow the creation of lockfiles.
val unsetLock = ComposerCommand.run(workingDir, "--no-interaction", "config", "--unset", "lock")
if (unsetLock.isError) {
definitionFileBackup?.delete()
return null
}
}

return definitionFileBackup
}

private fun createLockFile(): Boolean {
val args = buildList {
add("--no-interaction")
add("update")
add("--ignore-platform-reqs")

val composerVersion = Semver(ComposerCommand.getVersion(workingDir))
if (composerVersion.major >= 2) {
add("--no-install")
add("--no-audit")
}
}

val update = ComposerCommand.run(workingDir, *args.toTypedArray())
return update.isSuccess
}
}
Loading