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

Add warning for ignored aspects #7332

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -18,16 +18,19 @@ package com.google.idea.blaze.base.sync.aspects.storage
import com.google.idea.blaze.base.buildview.println
import com.google.idea.blaze.base.scope.BlazeContext
import com.google.idea.blaze.base.scope.Scope
import com.google.idea.blaze.base.scope.output.IssueOutput
import com.google.idea.blaze.base.scope.scopes.ToolWindowScope
import com.google.idea.blaze.base.settings.BlazeImportSettings
import com.google.idea.blaze.base.settings.BlazeImportSettingsManager
import com.google.idea.blaze.base.sync.SyncScope.SyncFailedException
import com.google.idea.blaze.base.sync.data.BlazeDataStorage
import com.google.idea.blaze.base.sync.data.BlazeProjectDataManager
import com.google.idea.blaze.base.toolwindow.Task
import com.google.idea.blaze.common.Label
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.Service
import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.project.Project
import kotlinx.coroutines.CoroutineScope
import java.io.IOException
Expand All @@ -38,6 +41,10 @@ import java.util.*
private const val ASPECT_TASK_TITLE = "Write Aspects"
private const val ASPECT_DIRECTORY = "aspects"

private const val BAZEL_IGNORE_FILE = ".bazelignore"

private val LOG = logger<AspectStorageService>()

@Service(Service.Level.PROJECT)
class AspectStorageService(private val project: Project, private val scope: CoroutineScope) {

Expand Down Expand Up @@ -72,6 +79,8 @@ class AspectStorageService(private val project: Project, private val scope: Coro

ctx.println("Writing aspects to $directory")

detectBazelIgnoreAndEmitWarning(ctx, directory)

try {
if (!Files.exists(directory)) {
Files.createDirectories(directory)
Expand Down Expand Up @@ -114,4 +123,36 @@ class AspectStorageService(private val project: Project, private val scope: Coro
// if this is not the case, fallback to .ijwb_aspects or .clwb_aspects
return workspacePath.resolve(BlazeDataStorage.PROJECT_DATA_SUBDIRECTORY + "_aspects")
}

/**
* The aspects should not be in the bazel ignore file. Emit a warning if any subpath of the aspect directory is
* mentioned in the bazel ignore file.
*/
private fun detectBazelIgnoreAndEmitWarning(ctx: BlazeContext, aspectPath: Path) {
val projectData = BlazeProjectDataManager.getInstance(project).getBlazeProjectData() ?: return

val bazelIgnore = projectData.workspacePathResolver.resolveToFile(BAZEL_IGNORE_FILE).toPath()
if (!Files.exists(bazelIgnore)) return

val aspectFile = aspectPath.toFile()
val aspectRelativePath = projectData.workspacePathResolver.getWorkspacePath(aspectFile)?.asPath() ?: return

val lines = try {
Files.lines(bazelIgnore).map(String::trim).toList()
} catch (e: IOException) {
LOG.warn("could not read bazel ignore file", e)
return
}

for (i in 1..aspectRelativePath.nameCount) {
if (!lines.contains(aspectRelativePath.subpath(0, i).toString())) continue

IssueOutput.warn("Aspects in $BAZEL_IGNORE_FILE file")
.withDescription("Please make sure that $aspectRelativePath is not covered by the $BAZEL_IGNORE_FILE file")
.withFile(bazelIgnore.toFile())
.submit(ctx)

break
}
}
}