Skip to content
Open
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
15 changes: 14 additions & 1 deletion src/classes/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,19 @@ void file_setworkingdirectory(const char *path) {
}
}

/** Gets the global current working directory (relative to the filing systems cwd.
* @param[out] path - path to working directory. */
void file_getworkingdirectory(varray_char *path)
{
if (workingdir.count>0) {
for (unsigned int i=0; i<workingdir.count && workingdir.data[i]!='\0'; i++) {
varray_charwrite(path, workingdir.data[i]);
}
varray_charwrite(path, MORPHO_DIRSEPARATOR);
}
varray_charwrite(path, '\0');
}

/** Gets the relative path for a given file */
void file_relativepath(const char *fname, varray_char *name) {
/* Check the fname passed isn't a global file reference (i.e. starts with / or ~) */
Expand All @@ -119,7 +132,7 @@ void file_relativepath(const char *fname, varray_char *name) {
for (unsigned int i=0; i<workingdir.count && workingdir.data[i]!='\0'; i++) {
varray_charwrite(name, workingdir.data[i]);
}
varray_charwrite(name, '/');
varray_charwrite(name, MORPHO_DIRSEPARATOR);
}
}
varray_charadd(name, (char *) fname, (int) strlen(fname));
Expand Down
3 changes: 2 additions & 1 deletion src/classes/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ typedef struct {
* ------------------------------------------------------- */

bool file_getsize(FILE *f, size_t *s);
void file_setworkingdirectory(const char *script);
void file_setworkingdirectory(const char *path);
void file_getworkingdirectory(varray_char *path);
FILE *file_openrelative(const char *fname, const char *mode);
int file_readlineintovarray(FILE *f, varray_char *string);
bool file_readintovarray(FILE *f, varray_char *string);
Expand Down
15 changes: 15 additions & 0 deletions src/core/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -4805,6 +4805,16 @@ static codeinfo compiler_import(compiler *c, syntaxtreenode *node, registerindx
goto compiler_import_cleanup;
}

/* Change working directory to new file for duration of import compilation */
varray_char wrkdir;
varray_charinit(&wrkdir);
file_getworkingdirectory(&wrkdir);
varray_char fpath;
varray_charinit(&fpath);
varray_charadd(&fpath, wrkdir.data, wrkdir.count-1);
varray_charadd(&fpath, fname, (int) strlen(fname));
file_setworkingdirectory(fpath.data);

/* Remember the initial position of the code */
start=c->out->code.count;

Expand Down Expand Up @@ -4841,6 +4851,11 @@ static codeinfo compiler_import(compiler *c, syntaxtreenode *node, registerindx
debugannotation_setmodule(&c->out->annotations, compiler_getmodule(c));

end=c->out->code.count;

/* Restore old working directory */
file_setworkingdirectory(wrkdir.data);
varray_charclear(&wrkdir);
varray_charclear(&fpath);

compiler_clear(&cc);
varray_charclear(&src);
Expand Down
5 changes: 5 additions & 0 deletions test/import/relative_directory.morpho
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import "relative_directory/target.m"
// expect: Successfully imported target

folderfn("Hello world")
// expect: Hello world
3 changes: 3 additions & 0 deletions test/import/relative_directory/inner_target.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn folderfn(message) {
print message
}
3 changes: 3 additions & 0 deletions test/import/relative_directory/target.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import "inner_target.m"

print "Successfully imported target"
Loading