Skip to content

Commit

Permalink
Use -batch and -no-colors for sbt for compatibility with older SB…
Browse files Browse the repository at this point in the history
…T. (#1356)

* Use `-batch` and `-no-colors` for sbt for compatibility with older SBT.

* Helper for generating sbt commands with the right option syntax.

* Changelog.

* Add integration test for old scala.

* Fix warning.

* Install "sbt".

* JDK 11

* Remove old sbt integration test. It more than doubled the time to run the tests.
  • Loading branch information
csasarak committed Jan 11, 2024
1 parent a48355e commit 215a74f
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 45 deletions.
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# FOSSA CLI Changelog

## v3.8.30

- Fix an issue with long-option syntax for older versions of `sbt` ([#1356](https://github.com/fossas/fossa-cli/pull/1356))
- Debug: add more logging for debugging missing dependencies. ([#1360](https://github.com/fossas/fossa-cli/pull/1360))

## v3.8.29
Expand Down
6 changes: 3 additions & 3 deletions integration-test/Analysis/ScalaSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
module Analysis.ScalaSpec (spec) where

import Analysis.FixtureExpectationUtils (
DependencyResultsSummary (DependencyResultsSummary),
DependencyResultsSummary (..),
testSuiteDepResultSummary,
)
import Analysis.FixtureUtils (
AnalysisTestFixture (AnalysisTestFixture),
FixtureArtifact (FixtureArtifact),
AnalysisTestFixture (..),
FixtureArtifact (..),
FixtureEnvironment (NixEnv),
)
import Path (reldir)
Expand Down
11 changes: 2 additions & 9 deletions src/Strategy/Scala.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import Discovery.Walk (
walkWithFilters',
)
import Effect.Exec (
AllowErr (Never),
Command (..),
Exec,
Has,
Expand All @@ -64,6 +63,7 @@ import Strategy.Maven.Pom qualified as Pom
import Strategy.Maven.Pom.Closure (MavenProjectClosure, buildProjectClosures, closurePath)
import Strategy.Maven.Pom.PomFile (RawPom (rawPomArtifact, rawPomGroup, rawPomVersion))
import Strategy.Maven.Pom.Resolver (buildGlobalClosure)
import Strategy.Scala.Common (mkSbtCommand)
import Strategy.Scala.Errors (FailedToListProjects (FailedToListProjects), MaybeWithoutDependencyTreeTask (MaybeWithoutDependencyTreeTask), MissingFullDependencyPlugin (MissingFullDependencyPlugin))
import Strategy.Scala.Plugin (genTreeJson, hasDependencyPlugins)
import Strategy.Scala.SbtDependencyTree (SbtArtifact (SbtArtifact), analyze, sbtDepTreeCmd)
Expand Down Expand Up @@ -230,14 +230,7 @@ analyzeWithSbtDepTree (ScalaProject maybeDepTree _ closure) = context "Analyzing
pure $ SbtArtifact groupId artifactId version

makePomCmd :: Command
makePomCmd =
Command
{ cmdName = "sbt"
, -- --no-colors to disable ANSI escape codes
-- --batch to disable interactivity. normally, if an `sbt` command fails, it'll drop into repl mode: --batch will disable the repl.
cmdArgs = ["--no-colors", "--batch", "makePom"]
, cmdAllowErr = Never
}
makePomCmd = mkSbtCommand "makePom"

genPoms :: (Has Exec sig m, Has ReadFS sig m, Has Diagnostics sig m) => Path Abs Dir -> m [MavenProjectClosure]
genPoms projectDir = do
Expand Down
20 changes: 20 additions & 0 deletions src/Strategy/Scala/Common.hs
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
module Strategy.Scala.Common (
removeLogPrefixes,
SbtArtifact (..),
mkSbtCommand,
) where

import Data.Maybe (fromMaybe)
import Data.Text (Text)
import Data.Text qualified as Text
import Effect.Exec (AllowErr (Never), Command (..))

-- | Generate an sbt sub-command using which turns off colored output and ensures it does not enter repl mode.
--
-- example:
-- mkSbtCommand "dependencyTree" results in "sbt -batch -no-colors dependencyTree" as a CLI command.
mkSbtCommand :: Text -> Command
mkSbtCommand cmdName =
Command
{ cmdName = "sbt"
, -- Use single hyphens rather than double-hyphens for old sbt compatibility.
-- Ex: -batch instead of --batch
cmdArgs =
[ "-batch"
, "-no-colors"
, cmdName
]
, cmdAllowErr = Never
}

data SbtArtifact = SbtArtifact
{ groupId :: Text
Expand Down
35 changes: 14 additions & 21 deletions src/Strategy/Scala/Plugin.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,18 @@ import Data.String.Conversion (ConvertUtf8 (decodeUtf8), toString)
import Data.Text qualified as Text
import Data.Text.Lazy qualified as TextLazy
import Effect.Exec (
AllowErr (Never),
Command (..),
Exec,
Has,
execThrow,
)
import Path (Abs, Dir, File, Path, mkRelFile, parent, parseAbsFile, (</>))
import Strategy.Scala.Common (mkSbtCommand)

-- | Returns list of plugins used by sbt.
-- Ref: https://www.scala-sbt.org/1.x/docs/Plugins.html
getPlugins :: Command
getPlugins =
Command
{ cmdName = "sbt"
, cmdArgs =
[ "--no-colors" -- to disable ANSI escape codes
, "--batch" -- to disable interactivity
, "plugins"
]
, cmdAllowErr = Never
}
getPlugins = mkSbtCommand "plugins"

-- | Returns list of plugins used by sbt.
hasDependencyPlugins :: (Has Exec sig m, Has Diagnostics sig m) => Path Abs Dir -> m (Bool, Bool)
Expand All @@ -54,17 +45,19 @@ hasDependencyPlugins projectDir = do
-- ./tree.json
-- ./tree.html
-- ./tree.data.js
--
-- This command is documented as being invoked with capital "HTML" but older versions of sbt output an error like:
--
-- [error] Not a valid command: dependencyBrowseTreeHTML
-- [error] Not a valid project ID: dependencyBrowseTreeHTML
-- [error] Expected ':'
-- [error] Not a valid key: dependencyBrowseTreeHTML (similar: dependencyBrowseTreeHtml, dependencyBrowseTree, dependencyBrowseTreeTarget)
-- [error] dependencyBrowseTreeHTML
--
-- This command is only used when the plugin is installed explicitly, i.e. sbt < 1.4.
-- Newer versions of sbt will use the built-in dependency graph plugin.
mkDependencyBrowseTreeHTMLCmd :: Command
mkDependencyBrowseTreeHTMLCmd =
Command
{ cmdName = "sbt"
, cmdArgs =
[ "--no-colors" -- to disable ANSI escape codes
, "--batch" -- to disable interactivity
, "dependencyBrowseTreeHTML"
]
, cmdAllowErr = Never
}
mkDependencyBrowseTreeHTMLCmd = mkSbtCommand "dependencyBrowseTreeHtml"

genTreeJson :: (Has Exec sig m, Has Diagnostics sig m) => Path Abs Dir -> m [Path Abs File]
genTreeJson projectDir = do
Expand Down
14 changes: 2 additions & 12 deletions src/Strategy/Scala/SbtDependencyTree.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@ import DepTypes (
VerConstraint (CEq),
)
import Effect.Exec (
AllowErr (Never),
Command (..),
Exec,
ExecErr (CommandParseError),
)
import Graphing (Graphing, shrinkRoots, subGraphOf, unfold)
import Strategy.Scala.Common (SbtArtifact (SbtArtifact, artifactId, groupId, version), removeLogPrefixes)
import Strategy.Scala.Common (SbtArtifact (SbtArtifact, artifactId, groupId, version), mkSbtCommand, removeLogPrefixes)
import Text.Megaparsec (
MonadParsec (eof, takeWhileP, try),
Parsec,
Expand All @@ -57,16 +56,7 @@ import Text.Megaparsec.Char.Lexer qualified as Lexer
-- This only works with sbt v1.4.0 greater, or with sbt which has DependencyTreePlugin.
-- Ref: https://www.scala-sbt.org/1.x/docs/sbt-1.4-Release-Notes.html#sbt-dependency-graph+is+in-sourced
sbtDepTreeCmd :: Command
sbtDepTreeCmd =
Command
{ cmdName = "sbt"
, cmdArgs =
[ "--batch" -- ensure sbt does not enter repl mode!
, "--no-colors"
, "dependencyTree"
]
, cmdAllowErr = Never
}
sbtDepTreeCmd = mkSbtCommand "dependencyTree"

data SbtDep = SbtDep
{ artifact :: SbtArtifact
Expand Down

0 comments on commit 215a74f

Please sign in to comment.