Skip to content

Commit 7d11ef1

Browse files
committed
fix for validation warnings
1 parent ee646b4 commit 7d11ef1

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

pkg/api/api_impl.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,6 @@ func contextImpl(buildOpts BuildOptions) (*internalContext, []Message) {
903903
LogLevel: validateLogLevel(buildOpts.LogLevel),
904904
Overrides: validateLogOverrides(buildOpts.LogOverride),
905905
}
906-
log := logger.NewStderrLog(logOptions)
907906

908907
// Validate that the current working directory is an absolute path
909908
absWorkingDir := buildOpts.AbsWorkingDir
@@ -916,6 +915,7 @@ func contextImpl(buildOpts BuildOptions) (*internalContext, []Message) {
916915
DoNotCache: true,
917916
})
918917
if err != nil {
918+
log := logger.NewStderrLog(logOptions)
919919
log.AddError(nil, logger.Range{}, err.Error())
920920
return nil, convertMessagesToPublic(logger.Error, log.Done())
921921
}
@@ -924,6 +924,7 @@ func contextImpl(buildOpts BuildOptions) (*internalContext, []Message) {
924924
// directory doesn't change, since breaking that invariant would break the
925925
// validation that we just did above.
926926
caches := cache.MakeCacheSet()
927+
log := logger.NewDeferLog(logger.DeferLogNoVerboseOrDebug, logOptions.Overrides)
927928
onEndCallbacks, onDisposeCallbacks, finalizeBuildOptions := loadPlugins(&buildOpts, realFS, log, caches)
928929
options, entryPoints := validateBuildOptions(buildOpts, log, realFS)
929930
finalizeBuildOptions(&options)
@@ -933,7 +934,19 @@ func contextImpl(buildOpts BuildOptions) (*internalContext, []Message) {
933934

934935
// If we have errors already, then refuse to build any further. This only
935936
// happens when the build options themselves contain validation errors.
936-
if msgs := log.Done(); log.HasErrors() {
937+
msgs := log.Done()
938+
if log.HasErrors() {
939+
if logOptions.LogLevel < logger.LevelSilent {
940+
// Print all deferred validation log messages to stderr. We defer all log
941+
// messages that are generated above because warnings are re-printed for
942+
// every rebuild and we don't want to double-print these warnings for the
943+
// first build.
944+
stderr := logger.NewStderrLog(logOptions)
945+
for _, msg := range msgs {
946+
stderr.AddMsg(msg)
947+
}
948+
stderr.Done()
949+
}
937950
return nil, convertMessagesToPublic(logger.Error, msgs)
938951
}
939952

@@ -942,6 +955,7 @@ func contextImpl(buildOpts BuildOptions) (*internalContext, []Message) {
942955
onEndCallbacks: onEndCallbacks,
943956
onDisposeCallbacks: onDisposeCallbacks,
944957
logOptions: logOptions,
958+
logWarnings: msgs,
945959
entryPoints: entryPoints,
946960
options: options,
947961
mangleCache: buildOpts.MangleCache,
@@ -1423,6 +1437,7 @@ type rebuildArgs struct {
14231437
onEndCallbacks []onEndCallback
14241438
onDisposeCallbacks []func()
14251439
logOptions logger.OutputOptions
1440+
logWarnings []logger.Msg
14261441
entryPoints []bundler.EntryPoint
14271442
options config.Options
14281443
mangleCache map[string]interface{}
@@ -1440,6 +1455,11 @@ type rebuildState struct {
14401455
func rebuildImpl(args rebuildArgs, oldSummary buildSummary) rebuildState {
14411456
log := logger.NewStderrLog(args.logOptions)
14421457

1458+
// All validation warnings are repeated for every rebuild
1459+
for _, msg := range args.logWarnings {
1460+
log.AddMsg(msg)
1461+
}
1462+
14431463
// Convert and validate the buildOpts
14441464
realFS, err := fs.RealFS(fs.RealFSOptions{
14451465
AbsWorkingDir: args.absWorkingDir,

scripts/js-api-tests.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5863,7 +5863,7 @@ let transformTests = {
58635863
assert.strictEqual(code, `(() => {\n const import_meta = {};\n console.log(import_meta, import_meta.foo);\n})();\n`)
58645864
},
58655865

5866-
async defineIdentifierVsStringWarningIssue466({ esbuild }) {
5866+
async defineIdentifierVsStringWarningIssue466Transform({ esbuild }) {
58675867
const { warnings } = await esbuild.transform(``, { define: { 'process.env.NODE_ENV': 'production' } })
58685868
const formatted = await esbuild.formatMessages(warnings, { kind: 'warning' })
58695869
assert.strictEqual(formatted[0],
@@ -5874,6 +5874,20 @@ let transformTests = {
58745874
│ ~~~~~~~~~~~~
58755875
╵ '"production"'
58765876
5877+
`)
5878+
},
5879+
5880+
async defineIdentifierVsStringWarningIssue466Build({ esbuild }) {
5881+
const { warnings } = await esbuild.build({ define: { 'process.env.NODE_ENV': 'production' }, logLevel: 'silent' })
5882+
const formatted = await esbuild.formatMessages(warnings, { kind: 'warning' })
5883+
assert.strictEqual(formatted[0],
5884+
`▲ [WARNING] "process.env.NODE_ENV" is defined as an identifier instead of a string (surround "production" with quotes to get a string) [suspicious-define]
5885+
5886+
<js>:1:34:
5887+
1 │ define: { 'process.env.NODE_ENV': 'production' }
5888+
│ ~~~~~~~~~~~~
5889+
╵ '"production"'
5890+
58775891
`)
58785892
},
58795893

0 commit comments

Comments
 (0)