Commit Graph

10242 Commits

Author SHA1 Message Date
Ethan Atkins 6dd69a54ae Close line reader when interrupted
There are cases where if the ui state is changing rapidly, that an
AskUserThread can be created and cancelled in a short time windows. This
could cause problems if the AskUserThread is interrupted during
`LineReader.createReader` which I think can shell out to run some
commands so it is relatively slow. If the thread was interrupted during
the call to `LineReader.createReader` and the interruption was not
handled, then the thread would go into `LineReader.readLine`, which
wouldn't exit until the user pressed enter. This ultimately caused the
ui to break until enter because this zombie line reader would be holding
the lock on the terminal input stream.
2020-08-09 16:33:46 -07:00
Ethan Atkins e4cd6a38fc Hold lock while writing bytes to stdout
We should always hold the print stream lock when calling
progressState.write because otherwise the task progress thread could
concurrently write to stdout.
2020-08-09 16:33:46 -07:00
Ethan Atkins 44a605198b Add toString implementation to ConsoleOut instances
This makes it easier to debug which ConsoleOut is printing output.
2020-08-09 16:33:46 -07:00
Ethan Atkins 9c0c51971e
Merge pull request #5731 from eatkins/logging-rework
Add internal multi logger implementation
2020-08-09 16:33:23 -07:00
Ethan Atkins 07acb2aa0e
Merge branch 'develop' into logging-rework 2020-08-09 15:55:46 -07:00
eugene yokota df1eae1a87
Merge pull request #5734 from eatkins/defines-class-cache
Cache jar classpath entries between commands
2020-08-09 15:04:59 -04:00
Ethan Atkins 2e9805b9d0 Add internal multi logger implementation
Prior to these changes, sbt was leaking large amounts of memory via
log4j appenders. sbt has an unusual use case for log4j because it
creates many ephemeral loggers while also having a global logger that is
supposed to work for the duration of the sbt session. There is a lot of
shared global state in log4j and properly cleaning up the ephemeral task
appenders would break global logging. This commit fixes the behavior by
introducing an alternate logging implementation. Users can still use the
old log4j logging implementation but it will be off by default. The
internal implementation is very simple: it just blocks the current
thread and writes to all of the appenders. Nevertheless, I found the
performance to be roughly identical to that of log4j in my sample
project. As an experiment, I did the appending on a thread pool and got
a significant performance improvement but I'll defer that to a later PR
since parallel io is harder to reason about.

Background: I was testing sbt performance in
https://github.com/jtjeferreira/sbt-multi-module-sample and noticed that
performance rapidly degraded after I ran compile a few times. I took a
heap dump and it became obvious that sbt was leaking console appenders.
Further investigation revealed that all of the leaking appenders in the
project were coming from task streams. This made me think that the fix
would be to track what loggers were created during task evaluation and
clear them out when task evaluation completed. That almost worked except
that log4j has an internal append only data structure containing logger
names. Since we create unique logger names for each run, that internal
data structure grew without bound. It looked like this could be worked
around by creating a new log4j Configuration (where that data structure
was stored) but while creating new configurations with each task runs
did fix the leak, it also broke global logging, which was using a
different configuration. At this point, I decided to write an alternate
implementation of the appender api where I could be sure that the
appenders were cleaned up without breaking global logging.

Implementation: I made ConsoleAppender a trait and made it no longer
extends log4j AbstractAppender. To do this, I had to remove the one
log4j specific method, append(LogEvent). ConsoleAppender now has a
method toLog4J that, in most cases, will return a log4j Appender that is
almost identical to the Appenders that we previously used. To manage
the loggers created during task evaluation, I introduce a new class,
LoggerContext. The LoggerContext determines which logging backend to use
and keeps track of what appenders and loggers have been created. We can
create a fresh LoggerContext before each task evaluation and clear it
out, cleaning up all of its resources after task evaluation concludes.

In order to make this work, there were many places where we need to
either pass in a LoggerContext or create a new one. The main magic is
happening in the `next(State)` method in Main. This is where we create a
new LoggerContext prior to command evaluation and clean it up after the
evaluation completes.

Users can toggle log4j using the new useLog4J key. They also can set the
system property, sbt.log.uselog4j. The global logger will use the sbt
internal implementation unless the system property is set.

There are a fairly significant number of mima issues since I changed the
type of ConsoleAppender. All of the mima changes were in the
sbt.internal package so I think this should be ok.

Effects: the memory leaks are gone. I successfully ran 5000 no-op
compiles in the sbt-multi-module-sample above with no degradation of
performace. There was a noticeable degradation after 30 no-op compiles
before.

During the refactor, I had to work on TestLogger and in doing so I also
fixed https://github.com/sbt/sbt/issues/4480.

This also should fix https://github.com/sbt/sbt/issues/4773
2020-08-09 11:20:34 -07:00
Ethan Atkins 0d2b00c7e9 Cache jar classpath entries between commands
Zinc frequently needs to check the library classpath to ensure that
class names are defined in a given jar. There is a cost to looking up
the class names in the jar so it's a benefit to cache this across runs
so that we don't have to redo the same work every time. More
importantly, in testing with the latest sbt HEAD, I found that sbt would
crash fairly frequently because it ran out of direct memory, which is
used by nio to read and write to native memory without copying. The
direct memory area is shared with the java heap and if it reaches the
limit, the jvm crashes hard as though kill -9 was invoked. After caching
the entries, I stopped seeing crashes.
2020-08-09 10:24:26 -07:00
Ethan Atkins 841b6dbfd8 Remove SetTerminal command
Rather than relying on a command, I realized it makes more sense to
explicitly set the terminal for the calling channel in MainLoop. By
doing it this way, we can also ensure that we always reset it to the
previous value.
2020-08-08 09:48:48 -07:00
Ethan Atkins 46724159da Stop filling json codec cache
These were not actually used as far as I could tell. The json codecs
cache showed up as taking up 30MB in a heap dump that I took after
running compile 30 times in a clone of the repro project in
https://github.com/sbt/sbt/issues/5508.
2020-08-08 09:02:38 -07:00
Ethan Atkins 6c50a85f93 Use a macro to create StringTypeTag instances
Using the scala reflect library always introduces significant
classloading overhead. We can eliminate the classloading overhead by
generating StringTypeTags at compile time instead.

This sped up average project loading time by a few hundred milliseconds
on my computer. The ManagedLoggedReporter in zinc is still using the
type tag based apis but after the next sbt release, we can upgrade the
zinc apis. We also could consider breaking binary compatibility.
2020-08-08 09:02:38 -07:00
eugene yokota 20f725c73c
Merge pull request #5707 from eatkins/native-install
Add installNativeExecutable task
2020-08-08 11:57:40 -04:00
Ethan Atkins b9cda2e713
Merge pull request #5729 from eatkins/scalacache
Use java caffeine library rather than scalacache
2020-08-07 18:06:11 -07:00
Ethan Atkins 525cff7fd7 Use java caffeine library rather than scalacache
sbt depends on scalacache (which hasn't been updated in about a year)
and we really don't need the functionality provided by scalacache. In
fact, the java api is somewhat easier to work with for our use case. The
motivation is that scalacache uses slf4j for logging which meant that it
was implicitly loading log4j. This caused some noisy logs during
shutdown when the previously unused cache was initialized just to be
cleaned up.

This commit also upgrades caffeine and moving forward we can always
upgrade caffeine (and potentially shade it) without any conflict with
the scalacache version.
2020-08-07 17:18:09 -07:00
eugene yokota 9beecf98e0
Merge pull request #5728 from eatkins/observer-memory-leak
Close file tree repository registrations
2020-08-07 18:29:16 -04:00
Ethan Atkins 05407cea55 Close file tree repository registrations
Upon successful registration with a FileTreeRepository, an Observable is
returned by the FileTreeRepository that can be used to observer the
specific globs that were registered. The FileTreeRepository also has a
global Observable that can be used to monitor _all_ events. In order to
implement this feature, internally the FileTreeRepository needs to hold
a reference to the registered Observable so that it forwards relevant
file change events. If we do not close the Observable, it leaks memory
inside of FileTreeRepository. There were a number of places within sbt
where we registered globs and did nothing with the returned Observable.
It was thus straightforward to fix the leak by just closing the returned
Observables.

This came up because I was looking at a heap dump of
https://github.com/jtjeferreira/sbt-multi-module-sample after running
1000 no-op compiles and noticed that the FileTreeRepository.observables
were taking up 75MB out of a total heap of about 300MB.

As a side note, it would be nice if sbt had a warning for unused return
values when a statement is not the last in a block. It's possible that
these leaks wouldn't have happened if we were forced to handle the
returned Observables.
2020-08-07 10:50:22 -07:00
eugene yokota 53299b79bd
Merge pull request #5724 from eed3si9n/wip/versionscheme
versionScheme setting + better eviction warning
2020-08-06 23:24:41 -04:00
eugene yokota a59d9fbb97
Merge branch 'develop' into wip/versionscheme 2020-08-06 21:04:21 -04:00
Ethan Atkins 48fa28e566 Add installNativeThinClient task
This allows a user to install the native thin client into a particular
directory (e.g. /usr/local/bin). I also made buildNativeThinClient have
a file dependency on the classpath so that it can be incremental if the
classpath hasn't changed. This is useful if the user has run
buildNativeThinClient for testing and then decides to install it once
it's been validated without having to rebuild (which takes a minimum of
about 30 seconds on my laptop).
2020-08-06 07:58:53 -07:00
eugene yokota 26d7331283
Merge pull request #5703 from eed3si9n/wip/pipelining
Build pipelining
2020-08-06 10:47:39 -04:00
Eugene Yokota 002f97cae7 Build pipelining
Ref https://github.com/sbt/zinc/pull/744

This implements `ThisBuild / usePipelining`, which configures subproject pipelining available from Zinc 1.4.0.

The basic idea is to start subproject compilation as soon as pickle JARs (early output) becomes available. This is in part enabled by Scala compiler's new flags `-Ypickle-java` and `-Ypickle-write`.

The other part of magic is the use of `Def.promise`:

```
earlyOutputPing := Def.promise[Boolean],
```

This notifies `compileEarly` task, which to the rest of the tasks would look like a normal task but in fact it is promise-blocked. In other words, without calling full `compile` task together, `compileEarly` will never return, forever waiting for the `earlyOutputPing`.
2020-08-06 02:31:01 -04:00
eugene yokota d1cd24078d
Merge pull request #5721 from eatkins/windows-appveyor
Attempt to make CI less flaky
2020-08-06 02:03:20 -04:00
eugene yokota f2d42264b6
Merge pull request #5708 from eatkins/internal-commands
Add fast path for parsing commands
2020-08-05 22:14:02 -04:00
Ethan Atkins 3a9d349065 Check server test project scalafmt on travis 2020-08-05 18:14:11 -07:00
Ethan Atkins 5e2fe77434 Disable server tests on windows ci
The server tests fail often which makes CI very painful.
2020-08-05 18:14:11 -07:00
Ethan Atkins caccba7112 Add fast path for parsing commands
It can easily take 2ms or more to parse a command depending on state's
combined parser. There are some commands that sbt requires to work that
we can handle in microseconds instead of milliseconds by special casing
them.

After this change, I saw the performance of
https://github.com/eatkins/scala-build-watch-performance improve by
a consistent 4-5ms in the 3 source file example which was a drop from
120ms to 115ms. While not necessarily earth shattering, this difference
could theoretically be much worse in other projects that have a lot of
plugins and custom tasks/commands. I think it's worth the modest
maintenance cost.
2020-08-05 17:19:05 -07:00
Eugene Yokota f947970247 versionScheme setting + better eviction warning
Ref https://github.com/sbt/sbt/issues/5710
Ref https://github.com/sbt/librarymanagement/pull/339

This adds `versionScheme` setting. When set, it is included into POM, and gets picked up on the other side as an extra attribute of ModuleID. That information in turn is used to inform the eviction warning.

This should reduce the false positives associated with SemVer'ed libraries showing up in the eviction warning.
2020-08-05 18:11:02 -04:00
eugene yokota 48f086059f
Merge pull request #5719 from eatkins/jline3-completions
Set complete flag in completions
2020-08-04 22:18:51 -04:00
eugene yokota 6c89d7416d
Merge pull request #5723 from eatkins/watch-state-map
Make watch implementation more sbt idiomatic
2020-08-04 22:17:31 -04:00
eugene yokota 34b74d0a14
Merge pull request #5718 from eatkins/force-flush
Allow sbt to force flush of remote output
2020-08-04 22:14:45 -04:00
eugene yokota ec626ee1db
Merge pull request #5720 from eatkins/whitespace
Apply miscellaneous whitespace changes
2020-08-04 22:13:48 -04:00
Ethan Atkins eb48f24f3a Make watch implementation more sbt idiomatic
The 1.4.0 implementation of watch uses a concurrent hash map to maintain
the global watch state which manages the state for an arbitrary number
of clients. Using a mutable map is not idiomatic sbt and I found it
difficult to reason about when the map was updated. This commit reworks
the feature so that the global state is instead stored in an immutable
map that is only modified during the internal watch commands, which is
easier to reason about.
2020-08-04 17:19:51 -07:00
Ethan Atkins 284ed4de5f Apply miscellaneous whitespace changes
The EventsTest changes kept appearing. I'm not sure why scalafmt check
was allowing it before. My vim status bar warns me about trailing spaces
and I noticed the two in Keys.scala and removed them.
2020-08-04 11:54:46 -07:00
Ethan Atkins 775cdd598a Catch IOExceptions in consoleLog
A ClosedChannelException was thrown here during CI.
2020-08-04 11:53:20 -07:00
Ethan Atkins edf43a473b Set complete flag in completions
JLine 3 automatically appends a space character to the completion
candidate unless you tell it not to by setting its 'complete' parameter.
This behavior is generally nice because it will automatically complete
something like 'foo/testO<TAB>' to 'foo/testOnly ' which allows the user
to start typing the testname without having to enter space. It does,
however, break scripted completions because it will complete
'scripted wat<TAB>' to 'scripted watch/ '

This commit updates the custom completer to append a " " to the initial
completions and check if there are any additional completions available.
If so, we set the complete flag to true and jline will append a space to
the input when the user presses <TAB> or <ENTER>. Otherwise the old
jline2 behavior where no spaces are ever appended is preserved.
2020-08-04 10:58:04 -07:00
Ethan Atkins b656d599e1 Allow sbt to force flush of remote output
In eb688c9ecd, we started buffering output
to the remote client to reduce flickering. This was causing problems
with the output for the thin client in batch mode. With the delay, it
was possible for the client to exit before all of its output had been
displayed.

Bonus: only display aggregation error message if terminal has success
enabled (the thin client displays its own timing message so the message
in aggregation ended up being a duplicate).
2020-08-04 10:44:43 -07:00
eugene yokota e76f61bec5
Merge pull request #5575 from eed3si9n/wip/bump
update to lm-coursier-shaded 2.0.0-RC6-8
2020-08-01 17:13:01 -04:00
Eugene Yokota 2bebee4dfe lm-coursier-shaded 2.0.0-RC6-8
https://github.com/coursier/sbt-coursier/releases/tag/v2.0.0-RC6-5
https://github.com/coursier/sbt-coursier/releases/tag/v2.0.0-RC6-6
https://github.com/coursier/sbt-coursier/releases/tag/v2.0.0-RC6-7
https://github.com/coursier/sbt-coursier/releases/tag/v2.0.0-RC6-8

Ref https://github.com/coursier/sbt-coursier/pull/235
Ref https://github.com/coursier/sbt-coursier/pull/262
2020-08-01 15:51:19 -04:00
eugene yokota 4963f22cc6
Merge pull request #5714 from eatkins/scripted-improvements
Reduce scripted ci duration
2020-08-01 13:39:57 -04:00
Ethan Atkins 1d08116a2e Stop instrumenting scripted
The commit 388ed641fb added an autoplugin
that was compiled for every scripted test. Compiling autoplugins
introduces a fair bit of overhead because it can easily take 3-4 seconds
to compile with a cold compiler and even a warn compiler takes a second
or so. Removing the instrumentation caused 3 tests to fail:

1. genereated-root-no-publish relied on setUpScripted modifing the project
   name. Explicitly setting the name in the project build.sbt fixed it.

2. cp-order I'm not sure why this change broke that test, but changing
   the coursier classpath ordering setting does not automatically
   trigger a reload on the next update. I have a more involved change
   that makes changing coursier settings invalidate the update cache
   but I'm markign the test as pending for now. It could be fixed by
   adding a call to `update` after
   `set csrConfiguration ~= (_.withClasspathOrder(false))` but I think
   it's better that the test actually reflect the expected behavior
   until I push the fix.

3. auto-plugins there was a hack that seemed added to address
   https://github.com/sbt/sbt/issues/3164. I cannot tell from either the
   issue or the linked PR what was going on and since removing the
   lines that were explicitly commented as being temporary fixed it,
   I figured it was ok to remove them.

This reverts commit e01f5f5ef1.
2020-07-31 18:56:04 -07:00
Ethan Atkins 50eed6d62e Disable pending dependency management tests
Pending tests really slow down scripted runs because they tend often
cause sbt to exit which means the next test has to reload the whole
build instead of just reloading. Unfortunately cold loading is still
pretty slow so this is significant overhead.

Before disabling these, the dependency-management suite took 335 seconds
on my computer. After, it dropped to 280 seconds.
2020-07-30 20:01:36 -07:00
eugene yokota 5afe8821d8
Merge pull request #5709 from eatkins/library-stamps
Don't use managedCache for library stamps
2020-07-29 14:11:49 -04:00
Ethan Atkins 7ba5fd79f8 Use time wrapped stamper for dependency classpath
It is expensive to compute the the hash of every jar on the classpath so
we can try to avoid that by using the timeWrappedStamper which only
computes the hash if the last modified time has changed.
2020-07-28 13:52:57 -07:00
Ethan Atkins c9dc041643 Don't use managedCache for library stamps
Using the managedCached introduced an unintended performance regression
because it ensured that we always computed the hash of each jar on the
dependency classpath. The backing ReadStamps only computes the stamp if
the timestamp of the jar has changed.
2020-07-28 13:50:49 -07:00
Eugene Yokota e66282433a Reproduce missingOk bug
Ref https://github.com/sbt/sbt/issues/4707
2020-07-28 16:33:28 -04:00
eugene yokota b345205c28
Merge pull request #5700 from eed3si9n/wip/bumpzinc
Drop unused Scala X-Ray (sxr) integration
2020-07-27 17:45:50 -04:00
eugene yokota 8e85035773
Merge pull request #5686 from eatkins/sbt-build-lint
Remove lint warnings in sbt build
2020-07-27 15:27:42 -04:00
eugene yokota 2072eba0a9
Merge pull request #5677 from adpi2/fix/exit-bsp-client
Exit BspClient after server socket is closed
2020-07-27 12:55:31 -04:00
Ethan Atkins 4698f388e9 Remove lint warnings in sbt build
There were a number of unused key lint warnings when loading the sbt
build. In the case of `fork in compile` and `crossVersion in update`, it
wasn't clear that these were actually used, so I removed those settings.
The others seemed to be used so I just added them to the exclude list.

To make this work with legacy versions of sbt, I redefined the
excludeLintKeys key. Once we update the build.properties to a 1.4.x
version, we can drop the `val excludeLint` definition and replace
`excludeLint` with `excludeLintKeys`.

Side note: ++= does not work with excludeLintKeys which is why I used +=
for the excludes.
2020-07-27 09:37:53 -07:00
Ethan Atkins 45a940a080 Add lintUnusedKeysOnLoad to lint ignore keys
I noticed that if lintUnusedKeysOnLoad := true is set, it emits a lint
warning.

As a side note, Project linting takes about 300-400ms in the sbt project
so we might want to consider disabling it by default in batch mode at
least.
2020-07-27 08:57:29 -07:00