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

Skip hot-reloading if build fails #480

Merged
merged 2 commits into from
Jun 8, 2024
Merged

Conversation

omochi
Copy link
Contributor

@omochi omochi commented Jun 8, 2024

課題

devコマンドはファイルの変更があるとリビルドをしますが、
この時、変更によってソースコードが壊れた場合、コンパイルに失敗することがあります。

しかし、プラグインはそれをフロントエンドに伝えていないため、
コンパイルに失敗したにも関わらず、
Build completed successfully と出力してからブラウザをリロードさせます。

ユーザがコードの編集中にこれが生じた場合、
successの表示と共にブラウザがリロードされたので、
ユーザは編集結果がブラウザにホットリロードされたと期待するでしょう。
しかし実際にはバイナリが更新されていないため、
動作確認がうまくいかず混乱するでしょう。

コンパイルエラーが出ていることは、
ターミナルの少し上を見なければわからず、
気づかない事があるでしょう。

提案

プラグインからフロントエンドにビルドが成功したかどうかを伝え、
失敗した場合にはそれを表示して、ブラウザのリロードはスキップします。

詳細

以下に変更前後の出力の変化を掲載します。
余計な空行も削除しています。

変更前, リビルド成功時

These paths have changed, rebuilding...
- /Users/omochi/github/swiftwasm/carton/Tests/Fixtures/SandboxApp/Sources/app
- /Users/omochi/github/swiftwasm/carton/Tests/Fixtures/SandboxApp/Sources/app

Build completed successfully

変更前, リビルド失敗時

These paths have changed, rebuilding...
- /Users/omochi/github/swiftwasm/carton/Tests/Fixtures/SandboxApp/Sources/app
- /Users/omochi/github/swiftwasm/carton/Tests/Fixtures/SandboxApp/Sources/app
Building for debugging...
[0/2] Write swift-version--1B43874C0F24DF5E.txt
[1/4] Write sources
[3/5] Compiling app main.swift
/Users/omochi/github/swiftwasm/carton/Tests/Fixtures/SandboxApp/Sources/app/main.swift:16:1: error: cannot find 'aaa' in scope
aaa
^~~


Build completed successfully

変更前, リビルド成功時

These paths have changed, rebuilding...
- /Users/omochi/github/swiftwasm/carton/Tests/Fixtures/SandboxApp/Sources/app
- /Users/omochi/github/swiftwasm/carton/Tests/Fixtures/SandboxApp/Sources/app
Build completed successfully
The app is currently hosted at http://127.0.0.1:8080/

変更前, リビルド失敗時

These paths have changed, rebuilding...
- /Users/omochi/github/swiftwasm/carton/Tests/Fixtures/SandboxApp/Sources/app
- /Users/omochi/github/swiftwasm/carton/Tests/Fixtures/SandboxApp/Sources/app
Building for debugging...
[0/2] Write swift-version--1B43874C0F24DF5E.txt
[1/4] Write sources
[3/5] Compiling app main.swift
/Users/omochi/github/swiftwasm/carton/Tests/Fixtures/SandboxApp/Sources/app/main.swift:16:1: error: cannot find 'aaa' in scope
aaa
^~~
Build failed

Copy link
Contributor Author

@omochi omochi left a comment

Choose a reason for hiding this comment

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

self comment

if !buildResult.succeeded {
Diagnostics.remark("[Plugin] **Build Failed**")
print(buildResult.logText)
print(buildResult.logText, terminator: "")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

ログが末尾改行を含んでいるので print の改行を除きます。

if !buildResult.succeeded {
Diagnostics.remark("[Plugin] **Build Failed**")
print(buildResult.logText)
print(buildResult.logText, terminator: "")
responseMessage = Data([0])
Copy link
Contributor Author

Choose a reason for hiding this comment

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

0: 失敗

} else {
Diagnostics.remark("[Plugin] **Build Succeeded**")
responseMessage = Data([1])
Copy link
Contributor Author

Choose a reason for hiding this comment

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

1: 成功

case .failedToOpenBuildRequestPipe: "failed to open build request pipe"
case .failedToOpenBuildResponsePipe: "failed to open build response pipe"
}
enum DevCommandError: Error & CustomStringConvertible {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

CartonFrontendDevCommand.Error だと型の外部から使うとき長いので、
トップレベルに移動して名前を簡潔にしました。

case noBuildResponseOption
case failedToOpenBuildRequestPipe
case failedToOpenBuildResponsePipe
case pluginConnectionClosed
Copy link
Contributor Author

Choose a reason for hiding this comment

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

パイプが閉じたエラーを追加

case failedToOpenBuildRequestPipe
case failedToOpenBuildResponsePipe
case pluginConnectionClosed
case brokenPluginResponse
Copy link
Contributor Author

Choose a reason for hiding this comment

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

プラグインから来たメッセージが壊れていた場合を追加

@@ -202,6 +206,20 @@ struct SwiftPMPluginBuilder: BuilderProtocol {
func run() async throws {
// We expect single response per request
try buildRequest.write(contentsOf: Data([1]))
_ = try buildResponse.read(upToCount: 1)
guard let responseMessage = try buildResponse.read(upToCount: 1) else {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

readの結果に応じて様々な例外を投げ分けます。

@@ -63,6 +63,10 @@ extension Event: Decodable {
}
}

public struct BuilderProtocolSimpleBuildFailedError: Error {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

現状、プラグイン側でコンパイラのエラーを出力していて、フロントエンド側では何も出す必要がありません。そのような、ただビルドができなかったことだけを伝えるためのエラー型を専用に定義します。

ビルドに失敗するのはある意味正常な通信の結果であり、
プラグインとの通信に失敗するなど、実際のビルド以前のレベルで失敗する場合もありえます。
これはエラー内容を出力します。

default:
terminal.write("\(error)\n", inColor: .red)
}
return
Copy link
Contributor Author

Choose a reason for hiding this comment

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

ビルドエラーの通知を受けたらその旨を表示します。
シンプルなビルドの失敗ではなかった場合は、その例外の情報も表示します。
そして脱出します。


terminal.write("\nBuild completed successfully\n", inColor: .green, bold: false)
terminal.write("Build completed successfully\n", inColor: .green)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

成功した場合はリロードさせます。

先頭の改行を消します。
デフォルトの non bold は引数指定が余計なので消します。

@omochi omochi marked this pull request as ready for review June 8, 2024 05:13
@kateinoigakukun kateinoigakukun changed the title devプラグインがビルドに成功したかどうかをdevフロントエンドに伝える Skip hot-reloading if build fails Jun 8, 2024
@kateinoigakukun kateinoigakukun merged commit 8c7ac43 into swiftwasm:main Jun 8, 2024
4 checks passed
@omochi omochi deleted the send-error branch June 8, 2024 11:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants