You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Summary: When saving a QGIS project, if a file with the same name as a layer's datasource exists in the current working directory, the layer's datasource is silently replaced. The layer then points to the file in the working directory instead of its original location.
The issue is caused by these two lines of code in qgsmaplayer.cpp:
The encodedSource method at Line 782 (whether it resolves to QgsRasterLayer::encodedSource, QgsVectorLayer::encodedSource, QgsPointCloudLayer::encodedSource or other) already calls to QgsProviderRegistry::instance()->absoluteToRelativeUri. As a result, the second call on line 783 is redundant. This leads to the QgsPathResolver::writePath method being called on on a path that is already relative, see here:
The srcFileInfo.canonicalFilePath() call resolves the path relative to the current working directory, not the project directory. This creates a problem when the working directory contains files with the same name as the layer datasources:
Windows users: The working directory is typically set to C:/Windows/System32 when QGIS is launched from the system menu. This reduces the likelihood of the issue occurring.
Linux users: The working directory is usually the user's home directory (/home/username). This directory often contains various files, increasing the chance of conflicts.
The working project directory in QGIS can be queried by a simple QDir::currentPath() call. To verify it in the running application, one can simply query QDir.currentPath() in the Python console, or Path('.').resolve().
Example scenario:
A Linux user has a file named points.geojson in their home directory.
They open a QGIS project located in /home/username/projects/demo/ that includes a relative datasource ./points.geojson.
When the project is saved, the datasource path is incorrectly resolved to ../../points.geojson, pointing to /home/username/points.geojson instead of the correct /home/username/projects/demo/points.geojson.
This happens silently during the save operation, leading to unexpected behavior.
Steps to reproduce the issue
Create a directory structure with two subfolders: WORKDIR and PROJDIR, both containing a file named points.geojson.
Create a new QGIS project in PROJDIR with a layer referencing ./points.geojson in that folder, save the project, and quit QGIS.
Launch QGIS with the working directory set to WORKDIR. On Linux, run QGIS from WORKDIR. On Windows, you can set the working directory by running QDir.setCurrent('/path/to/WORKDIR/) in the Python console.
Open the QGIS project in PROJDIR, save the project.
Observe that the layer's datasource path has changed to reference ../WORKDIR/points.geojson instead of the correct ./points.geojson in PROJDIR.
Versions
Verified on the 8e7781d dev version (3.41.0-Master) and on 3.40.2-Bratislava. Here's the version paste.
This issue is less likely to occur on Windows due to the default working directory. However, it is common in Linux workflows, when QGIS is launched from the user's home directory.
I have identified and fixed the issue in my local setup and will submit a pull request targeting 3.41.0-Master.
The text was updated successfully, but these errors were encountered:
vsydorov
added
the
Bug
Either a bug report, or a bug fix. Let's hope for the latter!
label
Jan 9, 2025
- The b9c1c2c commit introduced redundant absoluteToRelativeUri AND
relativeToAbsoluteUri calls. First one was removed earlier, stopping
the qgis#60100 issue. Remove the latter for completeness too.
- Minor indentation fix
What is the bug or the crash?
Summary: When saving a QGIS project, if a file with the same name as a layer's datasource exists in the current working directory, the layer's datasource is silently replaced. The layer then points to the file in the working directory instead of its original location.
The issue is caused by these two lines of code in
qgsmaplayer.cpp
:QGIS/src/core/qgsmaplayer.cpp
Lines 782 to 783 in 8cfdb1b
The encodedSource method at Line 782 (whether it resolves to
QgsRasterLayer::encodedSource
,QgsVectorLayer::encodedSource
,QgsPointCloudLayer::encodedSource
or other) already calls toQgsProviderRegistry::instance()->absoluteToRelativeUri
. As a result, the second call on line 783 is redundant. This leads to theQgsPathResolver::writePath
method being called on on a path that is already relative, see here:QGIS/src/core/qgspathresolver.cpp
Lines 292 to 294 in 8cfdb1b
The
srcFileInfo.canonicalFilePath()
call resolves the path relative to the current working directory, not the project directory. This creates a problem when the working directory contains files with the same name as the layer datasources:C:/Windows/System32
when QGIS is launched from the system menu. This reduces the likelihood of the issue occurring.QDir::currentPath()
call. To verify it in the running application, one can simply queryQDir.currentPath()
in the Python console, orPath('.').resolve()
.Example scenario:
points.geojson
in their home directory./home/username/projects/demo/
that includes a relative datasource./points.geojson.
../../points.geojson
, pointing to/home/username/points.geojson
instead of the correct/home/username/projects/demo/points.geojson
.Steps to reproduce the issue
WORKDIR
andPROJDIR
, both containing a file namedpoints.geojson
.PROJDIR
with a layer referencing./points.geojson
in that folder, save the project, and quit QGIS.QDir.setCurrent('/path/to/WORKDIR/)
in the Python console.PROJDIR
, save the project.../WORKDIR/points.geojson
instead of the correct./points.geojson
in PROJDIR.Versions
Verified on the 8e7781d dev version (3.41.0-Master) and on 3.40.2-Bratislava. Here's the version paste.
Supported QGIS version
New profile
Additional Context
This issue is less likely to occur on Windows due to the default working directory. However, it is common in Linux workflows, when QGIS is launched from the user's home directory.
I have identified and fixed the issue in my local setup and will submit a pull request targeting 3.41.0-Master.
The text was updated successfully, but these errors were encountered: