Skip to content

Bug: Rules do not catch imports from segments of other layers with the same slice name #18

@conarti

Description

@conarti

Description

Neither the layers-slices rule nor the public-api rule catches imports from segments of other layers when the slice name is the same in both layers.

Example

In a real project, the following code does not trigger any ESLint errors:

File: src/pages/policies/ui/PolicyNodeDetailsPage.vue

import { getNodePolicyById } from "@/entities/policies/api";
import { createNodePolicyFields } from "@/entities/policies/lib";
import type { PolicyDetailField } from "@/entities/policies/lib";

According to FSD methodology, the pages layer should only import from the entities layer through the public API (e.g., @/entities/policies), not directly from internal segments like @/entities/policies/api or @/entities/policies/lib.

Root Cause

The bug is in the isSameSlice calculation in src/lib/feature-sliced/extract-paths-info.ts:36-37:

const isSameSlice = target.validatedFeatureSlicedParts.hasSlice && currentFile.validatedFeatureSlicedParts.hasSlice
  && target.fsdParts.slice === currentFile.fsdParts.slice;

This logic only checks if the slice names are the same (policies === policies), but does not verify that they are in the same layer.

When importing from:

  • Current file: src/pages/policies/ui/PolicyNodeDetailsPage.vue (layer: pages, slice: policies)
  • Import target: @/entities/policies/api (layer: entities, slice: policies)

The condition isSameSlice becomes true because slice names match, even though the layers are different (pages !== entities).

This causes:

  1. The layers-slices rule to skip validation (via isNotSuitableForValidation in src/rules/layers-slices/model/is-not-suitable-for-validation.ts:14-16)
  2. The public-api rule to skip validation (via shouldBeFromSlicePublicApi in src/rules/public-api/model/should-be-from-public-api.ts:19-22)

Expected Behavior

The rules should report an error for these imports:

  • @conarti/feature-sliced/public-api should report: "Absolute imports are only allowed from public api"
  • Suggesting to use @/entities/policies instead

Suggested Fix

Modify the isSameSlice calculation to also check that the layers are the same:

const isSameSlice = target.validatedFeatureSlicedParts.hasSlice 
  && currentFile.validatedFeatureSlicedParts.hasSlice
  && target.fsdParts.layer === currentFile.fsdParts.layer  // Add this check
  && target.fsdParts.slice === currentFile.fsdParts.slice;

Configuration

ESLint config from the affected project:

featureSliced({
  publicApi: {
    level: "segments",
  },
}),
{
  rules: {
    "@conarti/feature-sliced/layers-slices": "warn",
  },
}

Additional Context

This issue affects projects where different layers use the same slice names (e.g., policies slice in both pages and entities layers), which is a common pattern in FSD architecture.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions