diff --git a/src/ModFile.cc b/src/ModFile.cc
index e11d5e153a29d8ffd0318c6bad84a583356d81af..1feca9efac6ea6c3d9345ec8d50be5c80c98e300 100644
--- a/src/ModFile.cc
+++ b/src/ModFile.cc
@@ -853,7 +853,9 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool clear_glo
          macOS), if we directly remove the "+" subdirectory, then the
          preprocessor is not able to recreate it afterwards (presumably because
          MATLAB maintains some sort of lock on it). The workaround is to rename
-         it before deleting it. */
+         it before deleting it (the renaming must occur in the same directory,
+         otherwise it may file if the destination is not on the same
+         filesystem). */
       if (filesystem::exists("+" + basename))
 	{
 	  auto tmp = unique_path();
@@ -1582,7 +1584,7 @@ ModFile::unique_path()
       string rand_str(rand_length, '\0');
       for (auto &dis : rand_str)
         dis = possible_characters[distribution(generator)];
-      path = filesystem::temp_directory_path() / rand_str;
+      path = rand_str;
     }
   while (filesystem::exists(path));
 
diff --git a/src/ModFile.hh b/src/ModFile.hh
index 5c3950d1677bd288a1c840bc4837bd23283edc7a..1bfe7cb7462f4a8167d94937d7fc990f3f6c1f67 100644
--- a/src/ModFile.hh
+++ b/src/ModFile.hh
@@ -131,7 +131,7 @@ private:
   void writeJsonComputingPassOutput(const string &basename, JsonFileOutputType json_output_mode, bool jsonderivsimple) const;
   void writeJsonFileHelper(const string &fname, ostringstream &output) const;
   vector<expr_t> pac_growth;
-  /* Generate a random temporary path. Equivalent to
+  /* Generate a random temporary path, in the current directory. Equivalent to
      boost::filesystem::unique_path(). Both are insecure, but currently there
      is no better portable solution. Maybe in a later C++ standard? */
   static filesystem::path unique_path();