Stabilize process execution and editor cleanup

This commit is contained in:
Krzosa Karol
2026-02-07 11:08:29 +01:00
parent 7fad476b61
commit 80df86df3d
6 changed files with 35 additions and 29 deletions

View File

@@ -726,8 +726,8 @@ API double GetTimeSeconds() {
return GetTimeMicros() / 1000000.0;
}
API String WriteTempFile(String data) {
Scratch scratch;
API String WriteTempFile(Allocator allocator, String data) {
Scratch scratch(allocator);
#if OS_WINDOWS
int buffer_len = MAX_PATH+1;
@@ -737,12 +737,12 @@ API String WriteTempFile(String data) {
Assert(result != 0);
String16 temp16 = {buffer, result};
NormalizePathInPlace(temp16);
String temp_directory = ToString(scratch, temp16);
String temp_directory = ToString(allocator, temp16);
#else
String temp_directory = "/tmp";
#endif
String temp_filename = Format(scratch, "%S/temp%llu", temp_directory, GetTimeNanos());
String temp_filename = Format(allocator, "%S/temp%llu", temp_directory, GetTimeNanos());
bool done = WriteFile(temp_filename, data);
Assert(done);
return temp_filename;

View File

@@ -32,6 +32,7 @@ bool IsFile(String path);
String GetWorkingDir(Allocator arena);
bool IsAbsolute(String path);
int64_t GetFileModTime(String file);
String WriteTempFile(Allocator allocator, String data);

View File

@@ -118,6 +118,7 @@ void LexDigit(Lexer *lex, Token *t) {
}
bool IsOkForIdent(Lexer *lex, char c) {
Unused(lex);
bool result = IsAlphanumeric(c) || c == '_' || c == '/';
return result;
}

View File

@@ -70,7 +70,7 @@ void Coro_SearchOpenBuffers(mco_coro *co) {
if (out_buffer == NULL) {
return;
}
Buffer *it = GetBuffer(id, NULL);
it = GetBuffer(id, NULL);
if (it == NULL) {
goto skip_buffer;
}

View File

@@ -233,22 +233,23 @@ Array<char *> SplitCommand(Allocator allocator, String command_line) {
return cmd;
}
Process SpawnProcess(String command_line, String working_dir, String write_stdin, Array<String> enviroment) {
Process SpawnProcess(ExecArgs args) {
Scratch scratch;
const int PIPE_READ = 0;
const int PIPE_WRITE = 1;
bool error = false;
working_dir = Copy(scratch, working_dir);
String working_dir = Copy(scratch, args.cwd);
chdir(working_dir.data);
Process process = {};
process.args = args;
UnixProcess *plat = (UnixProcess *)&process.platform;
Array<char *> args = SplitCommand(scratch, command_line);
Array<char *> args_cmd = SplitCommand(scratch, args.cmd);
Array<char *> env = {scratch};
char *exe = Copy(scratch, args.exe).data;
For (enviroment) {
For (args.env) {
Add(&env, Copy(scratch, it).data);
}
@@ -308,6 +309,7 @@ Process SpawnProcess(String command_line, String working_dir, String write_stdin
return process;
}
if (args.open_stdin) {
error = posix_spawn_file_actions_addclose(&actions, stdin_desc[PIPE_WRITE]) != 0;
if (error) {
Error("Libc function failed: posix_spawn_file_actions_addclose, with error: %s", strerror(errno));
@@ -319,9 +321,10 @@ Process SpawnProcess(String command_line, String working_dir, String write_stdin
Error("Libc function failed: posix_spawn_file_actions_adddup2 STDIN_FILENO, with error: %s", strerror(errno));
return process;
}
}
pid_t process_pid = 0;
error = posix_spawnp(&process_pid, args[0], &actions, NULL, args.data, env.data) != 0;
error = posix_spawnp(&process_pid, exe, &actions, NULL, args_cmd.data, env.data) != 0;
if (error) {
Error("Libc function failed: failed to create process\n, with error: %s", strerror(errno));
return process;
@@ -329,12 +332,9 @@ Process SpawnProcess(String command_line, String working_dir, String write_stdin
plat->child_stdout_read = stdout_desc[PIPE_READ];
plat->stdin_write = stdin_desc[PIPE_WRITE];
plat->pid = process_pid;
if (write_stdin.len) {
WriteStdin(&process, write_stdin);
CloseStdin(&process);
if (args.open_stdin) {
plat->stdin_write = stdin_desc[PIPE_WRITE];
}
process.id = process_pid;
@@ -431,8 +431,13 @@ String FindPython(Allocator allocator) {
for (int i = 0; i < Lengthof(tries); i += 1) {
For (paths) {
String path_it = Format(scratch, "%S/%S" IF_OS_WINDOWS_ELSE(".exe", ""), it, tries[i]);
#if OS_WINDOWS
bool is_bad_bad_ms = EndsWith(it, "AppData\\Local\\Microsoft\\WindowsApps") || EndsWith(it, "AppData/Local/Microsoft/WindowsApps");
if (FileExists(path_it) IF_OS_WINDOWS(&& !is_bad_bad_ms)) {
if (is_bad_bad_ms) {
continue;
}
#endif
if (FileExists(path_it)) {
return Copy(allocator, path_it);
}
}
@@ -446,7 +451,7 @@ void SetShell(Allocator allocator, String *exe, String *cmd, String in_cmd) {
*cmd = Format(allocator, "%S /C %S", *exe, in_cmd);
#else
*exe = "/usr/bin/bash";
String temp_file = WriteTempFile(in_cmd); // @todo: maybe try to pass by stdio here
String temp_file = WriteTempFile(allocator, in_cmd); // @todo: maybe try to pass by stdio here
*cmd = Format(allocator, "%S %S", *exe, temp_file);
#endif
}

View File

@@ -294,7 +294,6 @@ void TestInsertVariable() {
String a = "Thing/@(ProjectFolder)/Another";
String b = "Thing/@ProjectFolder/Another";
Assert(InsertVariables(scratch, a) == InsertVariables(scratch, b));
int c = 10;
} RegisterFunction(&TestFunctions, TestInsertVariable);
@@ -545,7 +544,7 @@ BSet Open(Window *window, String path, ResolveOpenMeta meta, bool set_active = t
if (o.use_python_shell == 1) {
args.exe = FindPython(scratch);
String temp_file = WriteTempFile(o.path);
String temp_file = WriteTempFile(scratch, o.path);
args.cmd = Format(scratch, "%S %S", args.exe, temp_file);
} else {
SetShell(scratch, &args.exe, &args.cmd, o.path);