@@ -2,7 +2,8 @@ import { TargetType } from '~/services/config'
22import { getImportObject , goRun } from '~/services/go'
33import { setTimeoutNanos , SECOND } from '~/utils/duration'
44import { instantiateStreaming } from '~/lib/go'
5- import client , { type BuildResponse , type EvalEvent , EvalEventKind } from '~/services/api'
5+ import { buildGoTestFlags , requiresWasmEnvironment } from '~/lib/sourceutil'
6+ import client , { type EvalEvent , EvalEventKind } from '~/services/api'
67import { isProjectRequiresGoMod , goModFile , goModTemplate } from '~/services/examples'
78
89import { type DispatchFn , type StateProvider } from '../helpers'
@@ -134,19 +135,6 @@ const fetchWasmWithProgress = async (dispatch: DispatchFn, fileName: string) =>
134135 }
135136}
136137
137- /**
138- * Returns command line args for Go test binary based on server build response.
139- */
140- const buildGoTestFlags = ( { isTest, hasBenchmark, hasFuzz } : BuildResponse ) : string [ ] => {
141- const flags : Array < [ string , boolean | undefined ] > = [
142- [ '-test.v' , isTest ] ,
143- [ '-test.bench=.' , hasBenchmark ] ,
144- [ '-test.fuzz=.' , hasFuzz ] ,
145- ]
146-
147- return flags . filter ( ( [ , keep ] ) => ! ! keep ) . map ( ( [ arg ] ) => arg )
148- }
149-
150138export const runFileDispatcher : Dispatcher = async ( dispatch : DispatchFn , getState : StateProvider ) => {
151139 dispatch ( newRemoveNotificationAction ( NotificationIDs . WASMAppExitError ) )
152140 dispatch ( newRemoveNotificationAction ( NotificationIDs . GoModMissing ) )
@@ -155,7 +143,7 @@ export const runFileDispatcher: Dispatcher = async (dispatch: DispatchFn, getSta
155143 const {
156144 settings,
157145 workspace,
158- runTarget : { target, backend } ,
146+ runTarget : { target : selectedTarget , backend } ,
159147 } = getState ( )
160148
161149 let { files, selectedFile } = workspace
@@ -205,7 +193,30 @@ export const runFileDispatcher: Dispatcher = async (dispatch: DispatchFn, getSta
205193 } )
206194 }
207195
208- switch ( target ) {
196+ // Force use WebAssembly for execution if source code contains go:build constraints.
197+ let runTarget = selectedTarget
198+ if ( runTarget !== TargetType . WebAssembly && requiresWasmEnvironment ( files ) ) {
199+ runTarget = TargetType . WebAssembly
200+ dispatch (
201+ newAddNotificationAction ( {
202+ id : NotificationIDs . GoTargetSwitched ,
203+ type : NotificationType . Warning ,
204+ title : 'Go environment temporarily changed' ,
205+ description : 'This program will be executed using WebAssembly as Go program contains "//go:build" tag.' ,
206+ canDismiss : true ,
207+ actions : [
208+ {
209+ key : 'ok' ,
210+ label : 'Ok' ,
211+ primary : true ,
212+ onClick : ( ) => dispatch ( newRemoveNotificationAction ( NotificationIDs . GoTargetSwitched ) ) ,
213+ } ,
214+ ] ,
215+ } ) ,
216+ )
217+ }
218+
219+ switch ( runTarget ) {
209220 case TargetType . Server : {
210221 // TODO: vet
211222 const res = await client . run ( files , false , backend )
@@ -239,7 +250,7 @@ export const runFileDispatcher: Dispatcher = async (dispatch: DispatchFn, getSta
239250 break
240251 }
241252 default :
242- dispatch ( newErrorAction ( `AppError: Unknown Go runtime type "${ target } "` ) )
253+ dispatch ( newErrorAction ( `AppError: Unknown Go runtime type "${ runTarget } "` ) )
243254 }
244255 } catch ( err : any ) {
245256 dispatch ( newErrorAction ( err . message ) )
0 commit comments