Skip to content

Commit

Permalink
change working directory before running bsc for ppxs
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredly committed Aug 13, 2018
1 parent 025aab1 commit 3f46094
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/analyze/AsYouType.re
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,18 @@ let justBscCommand = (~interface, compilerPath, sourceFile, includes, flags) =>
)
};

let runBsc = (~interface, compilerPath, sourceFile, includes, flags) => {
let runBsc = (~basePath, ~interface, compilerPath, sourceFile, includes, flags) => {
let cmd = justBscCommand(~interface, compilerPath, sourceFile, includes, flags);
Log.log("running bsc " ++ cmd);
let (out, error, success) = Commands.execFull(cmd);
Log.log("running bsc " ++ cmd ++ " with pwd " ++ basePath);
let (out, error, success) = Commands.execFull(~pwd=basePath, cmd);
if (success) {
Ok(out @ error)
} else {
Error(out @ error)
}
};

let process = (~uri, ~moduleName, text, ~cacheLocation, compilerPath, refmtPath, includes, flags) => {
let process = (~uri, ~moduleName, ~basePath, text, ~cacheLocation, compilerPath, refmtPath, includes, flags) => {
let interface = Utils.endsWith(uri, "i");
let%try (syntaxError, astFile) = switch (refmtPath) {
| Some(refmtPath) => runRefmt(~interface, ~moduleName, ~cacheLocation, text, refmtPath);
Expand All @@ -116,9 +116,9 @@ let process = (~uri, ~moduleName, text, ~cacheLocation, compilerPath, refmtPath,
Ok((None, astFile))
}
};
switch (runBsc(~interface, compilerPath, astFile, includes, flags)) {
switch (runBsc(~basePath, ~interface, compilerPath, astFile, includes, flags)) {
| Error(lines) => {
let cmtPath = cacheLocation /+ moduleName ++ ".cmt";
let cmtPath = cacheLocation /+ moduleName ++ ".cmt" ++ (interface ? "i" : "");
if (!Files.isFile(cmtPath)) {
Ok(TypeError(String.concat("\n", lines), SharedTypes.initFull(moduleName, uri)))
} else {
Expand All @@ -139,7 +139,7 @@ let process = (~uri, ~moduleName, text, ~cacheLocation, compilerPath, refmtPath,
}
}
| Ok(lines) => {
let cmt = Cmt_format.read_cmt(cacheLocation /+ moduleName ++ ".cmt");
let cmt = Cmt_format.read_cmt(cacheLocation /+ moduleName ++ ".cmt" ++ (interface ? "i" : ""));
let%try file = ProcessCmt.forCmt(uri, x => x, cmt);
let%try_wrap extra = ProcessExtra.forCmt(~file, cmt);
Success(lines, {file, extra})
Expand Down
1 change: 1 addition & 0 deletions src/analyze/State.re
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ let getCompilationResult = (uri, state, ~package) => {
let%try result = AsYouType.process(
~uri,
~moduleName,
~basePath=package.basePath,
text,
~cacheLocation=package.tmpPath,
package.compilerPath,
Expand Down
5 changes: 5 additions & 0 deletions src/analyze/core/ProcessExtra.re
Original file line number Diff line number Diff line change
Expand Up @@ -539,5 +539,10 @@ let forCmt = (~file, {cmt_modname, cmt_annots}: Cmt_format.cmt_infos) => switch
| Implementation(structure) => {
Ok(forItems(~file, structure.str_items, [||]))
}
| Partial_interface(_)
| Interface(_) => {
/** TODO actually process signature items */
Ok(forItems(~file, [], [||]))
}
| _ => Error("Invalid cmt file")
};
15 changes: 15 additions & 0 deletions src/util/Commands.re
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,22 @@ let execFull = (~input=?, ~pwd=?, ~env=Unix.environment(), cmd) => {
| None => env
| Some(pwd) => Array.map(item => String.length(item) > 4 && String.sub(item, 0, 4) == "PWD=" ? "PWD=" ++ pwd : item, env)
};
let prevCwd = switch pwd {
| None => None
| Some(pwd) =>
let prevCwd = Unix.getcwd();
if (prevCwd == pwd) {
None
} else {
Unix.chdir(pwd);
Some(prevCwd)
}
}
let (cmd_out, cmd_in, cmd_err) = Unix.open_process_full(cmd, env);
switch prevCwd {
| None => ()
| Some(prevCwd) => Unix.chdir(prevCwd)
};

switch input {
| None => ()
Expand Down
2 changes: 2 additions & 0 deletions tests/lib/TestUtils.re
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ let setUp = (files, text) => {
let%try_force result = AsYouType.process(
~uri,
~moduleName,
~basePath=package.basePath,
contents,
~cacheLocation=tmp,
"./node_modules/.bin/bsc",
Expand All @@ -129,6 +130,7 @@ let setUp = (files, text) => {
let%try_force result = AsYouType.process(
~uri=mainUri,
~moduleName="Test",
~basePath=package.basePath,
text,
~cacheLocation=tmp,
"./node_modules/.bin/bsc",
Expand Down

0 comments on commit 3f46094

Please sign in to comment.