Skip to content

fix JSON decoding of ConversionResult in playground #1073

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

Merged
merged 4 commits into from
Jul 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/Try.res
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ let default = props => {
let (isOverlayOpen, setOverlayOpen) = React.useState(() => false)

let lazyPlayground = Next.Dynamic.dynamic(
async () => await import(Playground.make),
async () => {
try {
await import(Playground.make)
} catch {
| JsExn(e) =>
Console.error2("Error loading Playground:", e)
JsExn.throw(e)
}
},
{
ssr: false,
loading: () => <span> {React.string("Loading...")} </span>,
Expand Down
3 changes: 3 additions & 0 deletions src/bindings/Node.res
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ module Process = {
@scope("process") external env: Dict.t<string> = "env"
@scope("process") @val external argv: array<string> = "argv"
@scope("process") external exit: int => unit = "exit"
module Env = {
@scope(("process", "env")) external nodeEnv: string = "NODE_ENV"
}
}

module Fs = {
Expand Down
96 changes: 39 additions & 57 deletions src/bindings/RescriptCompilerApi.res
Original file line number Diff line number Diff line change
Expand Up @@ -317,39 +317,29 @@ module CompileFail = {
let decode = (json): t => {
open JSON
switch json {
| Object(dict{"type": String(type_)}) =>
switch type_ {
| "syntax_error" =>
let locMsgs = switch json {
| Object(dict{"errors": Array(errors)}) => errors->Array.map(LocMsg.decode)
| _ => throw(Failure(`Failed to decode errors from syntax_error. ${__LOC__}`))
}
// TODO: There seems to be a bug in the ReScript bundle that reports
// back multiple LocMsgs of the same value
locMsgs->LocMsg.dedupe->SyntaxErr
| "type_error" =>
let locMsgs = switch json {
| Object(dict{"errors": Array(errors)}) => errors->Array.map(LocMsg.decode)
| _ => throw(Failure(`Failed to decode errors from type_error. ${__LOC__}`))
}
TypecheckErr(locMsgs)
| "warning_error" =>
let warnings = switch json {
| Object(dict{"errors": Array(warnings)}) => warnings->Array.map(Warning.decode)
| _ => throw(Failure(`Failed to decode errors from warning_error. ${__LOC__}`))
}
WarningErr(warnings)
| "other_error" =>
let locMsgs = switch json {
| Object(dict{"errors": Array(errors)}) => errors->Array.map(LocMsg.decode)
| _ => throw(Failure(`Failed to decode errors from other_error. ${__LOC__}`))
}
OtherErr(locMsgs)

| "warning_flag_error" => WarningFlagErr(WarningFlag.decode(json))
| other => throw(Failure(`Unknown type "${other}" in CompileFail result. ${__LOC__}`))
}
| _ => throw(Failure(`Failed to decode CompileFail. ${__LOC__}`))
| Object(dict{"type": String("syntax_error"), "errors": Array(errors)}) =>
let locMsgs = errors->Array.map(LocMsg.decode)
// TODO: There seems to be a bug in the ReScript bundle that reports
// back multiple LocMsgs of the same value
locMsgs->LocMsg.dedupe->SyntaxErr
| Object(dict{"type": String("type_error"), "errors": Array(errors)}) =>
let locMsgs = errors->Array.map(LocMsg.decode)
TypecheckErr(locMsgs)
| Object(dict{"type": String("warning_error"), "errors": Array(warnings)}) =>
let warnings = warnings->Array.map(Warning.decode)
WarningErr(warnings)
| Object(dict{"type": String("other_error"), "errors": Array(errors)}) =>
let locMsgs = errors->Array.map(LocMsg.decode)
OtherErr(locMsgs)
| Object(dict{"type": String("warning_flag_error")}) => WarningFlagErr(WarningFlag.decode(json))
| Object(dict{"type": String(other)}) =>
throw(Failure(`Unknown type "${other}" in CompileFail result. ${__LOC__}`))
| _ =>
throw(
Failure(
`Failed to decode CompileFail. ${__LOC__}. Could not decode \`${json->JSON.stringify}\``,
),
)
}
}
}
Expand All @@ -365,16 +355,9 @@ module CompilationResult = {
let decode = (~time: float, json: JSON.t): t => {
open JSON
switch json {
| Object(dict{"type": String(type_)}) =>
switch type_ {
| "success" => Success(CompileSuccess.decode(~time, json))
| "unexpected_error" =>
switch json {
| Object(dict{"msg": String(msg)}) => UnexpectedError(msg)
| _ => throw(Failure(`Failed to decode msg from unexpected_error. ${__LOC__}`))
}
| _ => Fail(CompileFail.decode(json))
}
| Object(dict{"type": String("success")}) => Success(CompileSuccess.decode(~time, json))
| Object(dict{"type": String("unexpected_error"), "msg": String(msg)}) => UnexpectedError(msg)
| Object(dict{"type": String(_)}) => Fail(CompileFail.decode(json))
| _ => throw(Failure(`Failed to decode CompilationResult. ${__LOC__}`))
}
}
Expand All @@ -390,20 +373,19 @@ module ConversionResult = {
let decode = (~fromLang: Lang.t, ~toLang: Lang.t, json): t => {
open JSON
switch json {
| Object(dict{
"type": String(type_),
"msg": ?Some(String(msg)),
"errors": ?Some(Array(errors)),
}) =>
switch type_ {
| "success" => Success(ConvertSuccess.decode(json))
| "unexpected_error" => msg->UnexpectedError
| "syntax_error" =>
let locMsgs = errors->Array.map(LocMsg.decode)
Fail({fromLang, toLang, details: locMsgs})
| other => Unknown(`Unknown conversion result type "${other}"`, json)
}
| _ => throw(Failure(`Failed to decode ConversionResult. ${__LOC__}`))
| Object(dict{"type": String("success")}) => Success(ConvertSuccess.decode(json))
| Object(dict{"type": String("unexpected_error"), "msg": String(msg)}) => UnexpectedError(msg)
| Object(dict{"type": String("syntax_error"), "errors": Array(errors)}) =>
let locMsgs = errors->Array.map(LocMsg.decode)
Fail({fromLang, toLang, details: locMsgs})
| Object(dict{"type": String(other)}) =>
Unknown(`Unknown conversion result type "${other}"`, json)
| _ =>
throw(
Failure(
`Failed to decode ConversionResult. ${__LOC__}. Could not decode \`${json->JSON.stringify}\``,
),
)
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/common/CompilerManagerHook.res
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ module LoadScript = {
}

module CdnMeta = {
let baseUrl = "/playground-bundles"
let baseUrl =
Node.Process.Env.nodeEnv === "development"
? "https://cdn.rescript-lang.org"
: "" + "/playground-bundles"

let getCompilerUrl = (version): string => `${baseUrl}/${Semver.toString(version)}/compiler.js`

Expand Down